Ruby 如何在Capybara单元测试中获得详细的错误消息
如何在Capybara单元测试中获得详细的错误消息Ruby 如何在Capybara单元测试中获得详细的错误消息,ruby,rspec,capybara,Ruby,Rspec,Capybara,如何在Capybara单元测试中获得详细的错误消息 describe "About" do it "should have the h1 'About Us'" do visit '/static_pages/about' page.should have_selector('h1', :text => "About Us") end it "should have the title 'About'" do visit '/static_pages/about' p
describe "About" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1',
:text => "About Us")
end
it "should have the title 'About'" do
visit '/static_pages/about'
page.should have_title("About")
end
这个测试的标题是“关于”
如何添加自定义错误消息,如:
Expected "About" but found "ABT". Please Rectify the mistake.
您可以添加“”中描述的自定义错误消息,如下所示:
it "should have the title 'About'" do
visit '/static_pages/about'
expect(page).to have_title("About"), lambda { "Expected 'About' but found '#{page.first("title", visible: false).native.text}'. Please Rectify the mistake."}
end
您可以添加“”中描述的自定义错误消息,如下所示:
it "should have the title 'About'" do
visit '/static_pages/about'
expect(page).to have_title("About"), lambda { "Expected 'About' but found '#{page.first("title", visible: false).native.text}'. Please Rectify the mistake."}
end
您可以添加如下所示的自定义错误消息,还应该添加截图以调试问题
describe "About" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1',
:text => "About Us")
end
it "should have the title 'About'" do
visit '/static_pages/about'
textToSearch="About"
begin
page.should have_title("#{textToSearch}")
rescue Exception => e
puts "Expected '#{textToSearch}' but found '#{page.first("title", visible: false).native.text}'. Please Rectify the mistake."
randomNumber=rand(100000)
page.save_screenshot("abc-#{randomNumber}.png",:full=>true)
raise e
end
end
希望这会有所帮助:)您可以添加如下所示的自定义错误消息,还应该添加截图以调试问题
describe "About" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1',
:text => "About Us")
end
it "should have the title 'About'" do
visit '/static_pages/about'
textToSearch="About"
begin
page.should have_title("#{textToSearch}")
rescue Exception => e
puts "Expected '#{textToSearch}' but found '#{page.first("title", visible: false).native.text}'. Please Rectify the mistake."
randomNumber=rand(100000)
page.save_screenshot("abc-#{randomNumber}.png",:full=>true)
raise e
end
end
希望这会有所帮助:)由于水豚的等待行为,您可能希望自定义消息是一个过程,以便在失败后而不是在搜索开始之前对其进行评估。非常好的建议@TomWalpole。我用lambda更新了我的答案。它显示错误:参数数目错误(1代表0)。有人知道如何添加到默认消息而不是替换它吗?我想指出,这对在页面上查找ID不起作用,我见过的唯一解决这个问题的方法是使用救援封锁,因为水豚的等待行为。你可能希望自定义消息是一个过程,这样它会在失败后而不是在搜索开始之前得到评估。非常好的建议@TomWalpole。我用lambda更新了我的答案。它显示了错误:错误的参数数(1代表0)有人知道如何添加到默认消息而不是替换它吗?我想指出,这对在页面上查找ID不起作用,我看到的使用该匹配器解决此问题的唯一方法是使用rescue块