javascript - Jasmine 装置和 jQuery .on()

标签 javascript jquery coffeescript jasmine jasmine-jquery

我的 jasmine 套件和 jQuery 新事件注册方法 .on() 的使用有问题。

这是我的装置的简化版本:

<div id='rent_payment_schedule_container'>
  <select class="select optional" id="frequency_select" name="payment_schedule[frequency]">
    <option value="0">Monthly</option>
    <option value="1">Weekly</option>
    <option value="2">Bi-Weekly</option>
  </select>

  <div class="control-group select optional start-day-select" id="monthly_select"></div>

  <div class="control-group select optional start-day-select" id="weekly_select" style="display: none;"></div>
</div>

这是 CoffeeScript (在使用它的实际页面上运行得很好):

$ ->
  $('#rent_payment_schedule_container').on 'change', '#frequency_select', (event) ->
    $('.start-day-select').hide()

    switch $(event.target).val()
      when '0'
        $('#monthly_select').show()
      when '1'
        $('#weekly_select').show()
      when '2'
        $('#weekly_select').show()

这是规范:

describe 'The UI components on the payment schedule creation page', ->
  beforeEach ->
    loadFixtures 'payment_schedule'

  describe 'The toggling of the monthly and weekly day select options', ->

    it 'shows the weekly select div and hides the monthly select div when the weekly option is selected from the #frequency_select select box', ->
      $('#frequency_select option[value=1]').attr('selected', 'selected')
      $('#frequency_select').change()
      expect($("#weekly_select")).toBeVisible()

    it 'shows the weekly select div and hides the monthly select div when the bi-weekly option is selected from the #frequency_select select box', ->
      $('#frequency_select option[value=2]').attr('selected', 'selected')
      $('#frequency_select').change()
      expect($("#weekly_select")).toBeVisible()

    it 'shows the monthly select div and hides the weekly select div when the monthly option is selected from the #frequency_select select box', ->
      $('#frequency_select option[value=1]').attr('selected', 'selected')
      $('#frequency_select').change()
      $('#frequency_select option[value=0]').attr('selected', 'selected')
      $('#frequency_select').change()
      expect($("#monthly_select")).toBeVisible()

而且每次都惨遭失败。

但是,如果不使用 $('#rent_payment_schedule_container')作为 .on() 的接收者,我用$(document) ,整个事情进展顺利。

$ ->
  $(document).on 'change', '#frequency_select', (event) ->
    $('.start-day-select').hide()

    switch $(event.target).val()
      when '0'
        $('#monthly_select').show()
      when '1'
        $('#weekly_select').show()
      when '2'
        $('#weekly_select').show()

所以,我最好的猜测是,这与 jasmine 加载夹具然后运行测试的顺序或速度有关,但我不能确定。谁能指出正确的方向,解释为什么会发生这种情况以及如何解决它?

最佳答案

我的猜测是您将脚本与规范运行程序一起加载。当 DOM 准备就绪时,您的脚本就会执行。不过此时夹具尚未加载。因此,$('#rent_ payment_schedule_container') 将不会选择任何元素,您可以自己验证这一点。

无论如何,您应该能够通过将脚本包装在可以在测试中调用的函数中来解决这个问题。

关于javascript - Jasmine 装置和 jQuery .on(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10645641/

相关文章:

javascript - 根据选择选项修改元素

javascript - 如何以百分比形式接收图像的值

javascript - 在 JS 中比较两个数组并显示错误索引

javascript - 引导主干应用程序

node.js - CoffeeScript 语法 {var}?

javascript - 通过 For 循环更改文本节点但导致意外的标识符

javascript - 如何从动态加载到我的 div 中的外部 html 文件运行 JavaScript 函数?

javascript - Vue.js 应用程序中生产模式下静态文件的路径错误

javascript - phonegap 页面重定向不起作用,对象的属性 'location' 不是函数

javascript - 未捕获的类型错误 : Cannot set property 'position' of undefined