测试JavaScript重定向的麻烦

我正在使用Ruby on Rails 3.1.0和rspec-rails 2 gem。 我想测试一个JavaScript重定向,但我这样做有些麻烦。

在我的控制器文件中,我有:

respond_to do |format| format.js { render :js => "window.location.replace('#{users_url}');" } end 

在我的spec文件中,我有:

 it "should redirect" do xhr :post, :create response.should redirect_to(users_url) end 

如果我运行规范,我会得到以下内容:

  Failure/Error: response.should redirect_to(users_url) Expected response to be a , but was  

如何实现规范代码以便正确测试JavaScript重定向?


解决方案的尝试

如果我使用

 response.should render_template("window.location.replace('#{users_url}');") 

我明白了

  Failure/Error: response.should render_template("window.location.replace('#{users_url}');") expecting <"window.location.replace('http:///users');"> but rendering with  

如果我使用

 response.should render_template(:js => "window.location.replace('#{users_url}');") 

规范成功通过,但太多了! 也就是说,我也可以说明以下内容,并且规范将(意外地)通过:

 response.should render_template(:js => "every_thing") 

我也看到了这个问题\答案和提议的解决方案有效,但我认为这是实现我的目标的最佳解决方案。

RSpec控制器规范使用rack_test驱动程序,该驱动程序不执行Javascript。 要测试使用Javascript进行重定向的页面,您应该真正查看另一个驱动程序。

与selenium-webdriver的Capybara做得很好。 RSpec请求规范替换了那些依赖于Javascript的测试的控制器规范。