bal*_*anv 35 rspec ruby-on-rails capybara
我刚学会了RSpec和Cabybara有多酷,现在正在努力学习写实际测试.
我试图检查点击链接后是否有重定向到特定页面.以下是场景
1) I have a page /projects/list
- I have an anchor with html "Back" and it links to /projects/show
Below is the test i wrote in rspec
describe "Sample" do
describe "GET /projects/list" do
it "sample test" do
visit "/projects/list"
click_link "Back"
assert_redirected_to "/projects/show"
end
end
end
测试失败并显示如下的失败消息
Failure/Error: assert_redirected_to "/projects/show"
ArgumentError:
@request must be an ActionDispatch::Request
请建议我如何测试重定向以及我做错了什么?
kov*_*ack 11
我不确定这可能是你需要的,但在我的测试中我更喜欢这样的方法:
...
subject { page }
...
before do
visit some_path_path
# do anything else you need to be redirected
end
it "should redirect to some other page" do
expect(page.current_path).to eq some_other_page_path
end
Run Code Online (Sandbox Code Playgroud)
在Rails中,当机架应用程序重定向时(就像Warden / Devise重定向您到登录页面一样),集成会话不会正确更新响应。结果,帮助程序assert_redirected_to将不起作用。
此页面也具有相同的信息:Rspec确保我们以正确的路径结束
因此,您需要测试您现在在该URL上,而不是测试您是否已重定向到该URL。
| 归档时间: |
|
| 查看次数: |
30812 次 |
| 最近记录: |