如何使用 rspec 和水豚测试 index/confirm/finish 流页面?
How to test index/confirm/finish flow pages with rspec and capybara?
我有三个页面:
- 指数
- 确认
- 完成
我在首页设置了文件上传功能。当用户附加文件并单击提交按钮时,我检查文件是否是一个好文件。
然后在确认页面显示验证消息。
如果没有任何错误,它将转到完成页面并显示成功消息。
现在我正在使用 rspec/capybara 进行功能测试。我可以测试用户在索引页中附加的是好文件还是坏文件。所以我可以想到确认页面会显示什么信息。
但是我不能先visit
确认页面或完成页面,因为我在这两个页面中都使用了http post方法。
那么如何在确认页面和完成页面进行功能测试?
在功能测试中,您不会单独测试这三个页面,而是将其作为一个功能进行测试,使用类似这些内容(包括按钮名称、消息、更改以匹配您的页面)
visit index_page_path # go to your index page
page.attach_file ... # select a file
page.click_button 'Submit' # submit the file
expect(page).to have_text 'Whatever message shows on the confirm page'
page.click_button 'Yes I want to save that file' # click the button on the confirm page to do whatever you're doing
expect(page).to have_text 'Whatever message shows on the finish page'
如果显示的 URL 页面对您很重要,您还可以在 have_text 方法之后测试 current_path。您将在 have_text 之后测试 current_path,因为 have_text 匹配器将等待提交完成并加载包含文本的新页面。如果您在浏览器仍然在上一页完成提交之前测试 current_path,测试将失败
您应该控制器测试 post 方法,而不是功能测试。像这样
describe SomeController, type: :controller do
it "uses post method to ..." do
post :index
expect(response.status).to eq 200
expect(response.body).to have_content('Hello World')
end
end
我有三个页面:
- 指数
- 确认
- 完成
我在首页设置了文件上传功能。当用户附加文件并单击提交按钮时,我检查文件是否是一个好文件。
然后在确认页面显示验证消息。
如果没有任何错误,它将转到完成页面并显示成功消息。
现在我正在使用 rspec/capybara 进行功能测试。我可以测试用户在索引页中附加的是好文件还是坏文件。所以我可以想到确认页面会显示什么信息。
但是我不能先visit
确认页面或完成页面,因为我在这两个页面中都使用了http post方法。
那么如何在确认页面和完成页面进行功能测试?
在功能测试中,您不会单独测试这三个页面,而是将其作为一个功能进行测试,使用类似这些内容(包括按钮名称、消息、更改以匹配您的页面)
visit index_page_path # go to your index page
page.attach_file ... # select a file
page.click_button 'Submit' # submit the file
expect(page).to have_text 'Whatever message shows on the confirm page'
page.click_button 'Yes I want to save that file' # click the button on the confirm page to do whatever you're doing
expect(page).to have_text 'Whatever message shows on the finish page'
如果显示的 URL 页面对您很重要,您还可以在 have_text 方法之后测试 current_path。您将在 have_text 之后测试 current_path,因为 have_text 匹配器将等待提交完成并加载包含文本的新页面。如果您在浏览器仍然在上一页完成提交之前测试 current_path,测试将失败
您应该控制器测试 post 方法,而不是功能测试。像这样
describe SomeController, type: :controller do
it "uses post method to ..." do
post :index
expect(response.status).to eq 200
expect(response.body).to have_content('Hello World')
end
end