使用 Rails 和 Rspec,有没有办法查看操作真正将您带到哪个页面?

With Rails and Rspec, is there a way to see what page an action really takes you to?

提问人:wruckie 提问时间:11/2/2023 更新时间:11/2/2023 访问量:31

问:

我有一个 rails 测试,在这个测试中,我转到一个页面并输入信息,保存该页面(这让我返回一页),然后尝试返回以查看数据是否仍然存在。我碰壁了,但怀疑如果我能看到页面上的内容,我就能辨别出错误之处或如何调整我的断言。有没有办法可视化/断点/卷曲测试实际遇到的页面?我断言的一切都是错误的。

it "Emergency Contact step saves data submitted" do
  click_link "Register online"
  click_link "Emergency Contacts"
  fill_in "Contact 1 Name", :with => "1 name"
  fill_in "Contact 1 Phone", :with => "123.456.7890"
  click_button "Save"
  click_link "Emergency Contacts"
  assert page.has_content?("1 name")
  assert page.has_content?("123.456.7890") 
end
Ruby-on-Rails 测试 rspec 断言

评论

1赞 wruckie 11/2/2023
@dbugger对我来说是失败的,但@Unixmonkey对我有用puts response.bodyputs page.html

答:

2赞 Unixmonkey 11/2/2023 #1

我在页面转换后使用的一种技术是检查当前路径(路由)与已知路径(路由),如下所示:

assert_equal page.current_path, new_page_path
# or
assert_equal page.current_path, "/things/new"

另一种调试方法是查看 like 的内容page.html

puts page.html

或者使用 capybara 的方法,将当前页面的 HTML 保存到 Rails 应用的目录中,并在浏览器中打开它。save_and_open_pagetmp

评论

1赞 wruckie 11/2/2023
puts page.html正是我需要的。我在正确的页面上,并且存在数据。显然,我使用了错误的断言来测试它。这是我的回应:<div title="Contact_1_name_div"> <label for="emer_con_person_1_name">Contact 1 Name</label><br /> <input type="text" value="1 name" /> </div>
0赞 wruckie 11/3/2023
作为记录,正确的断言是 stackoverflow.com/questions/10503802/......expect(page).to have_field('Contact 1 Name', with: '1 name')