黄瓜,jQuery和重定向

时间:2011-02-16 10:22:01

标签: ruby-on-rails testing cucumber capybara

我的页面上有一个select标签,我已经通过jQuery在domready上创建了一个更改事件的监听器。在此侦听器中,我使用url中的参数将用户重定向到同一页面。

当我测试功能手册时,它运行良好。

当我运行测试时,它失败了,重定向不起作用。

我如何才能使它发挥作用?

@javascript
  Scenario:
    When I select "2010" from "year_select"
    And I wait 5 seconds
    Then "2010" should be selected for "year_select"
    Then I should see "time_sheet?year=2010" with html

测试失败,我在html源代码中看到time_sheet?year = 2011(默认值),手动我看到了正确的值。它不是通过Javascript更新的,而是根据url中传递的te年变量(重定向之后)

我的jQuery代码:

jQuery(document).ready(function(){
  var yearSelect = jQuery("#year_select");
  yearSelect.change(function(){
    window.location = "/time_sheet_admin?year="+yearSelect.val();
  });
});

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为你错过了场景中的_admin。在场景中使用time_sheet_admin而不是time_sheet。如果我的建议对此毫无用处抱歉。

答案 1 :(得分:0)

我通常用于测试复杂JS的东西是将它们放在一个函数中,然后测试是否调用了该函数:

selectChanged = function() {
  window.location = "/time_sheet_admin?year="+jQuery("#year_select").val();
}

*注意不需要变量赋值,因为你只调用了一次。

然后在你的功能中:

Then I should see "time_sheet?year=2010" in the url

步骤定义:

When /Then I should see "time_sheet?year=2010" in the url/ do |seconds|
  page.execute_script("selectChanged()") 
end
相关问题