2011-02-16 89 views
0

我在我的頁面上有一個選擇標記,我已經通過jQuery在domready上創建了一個更改事件的偵聽器。在這個監聽器中,我使用url中的參數將用戶重定向到同一頁面。黃瓜,jQuery和重定向

當我測試功能手冊時,它工作得很好。

當我運行測試時,失敗,重定向不起作用。

我如何使它工作?

@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 

測試失敗,我看到time_sheet?年= 2011(默認值)在我的HTML源代碼,手動我看到正確的值。它不是通過JavaScript更新,但根據今年德中的變量傳遞的url(重定向後)

我的jQuery代碼:

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

我又有什麼錯?

+0

什麼的黃瓜步驟定義:那我應該能看到 「time_sheet年= 2010?」 的HTML? – corroded 2011-02-18 05:08:52

回答

0

我想你已經錯過了_admin的情況。在場景中使用time_sheet_admin而不是time_sheet。如果我的建議對此沒有用處。

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