2016-01-26 25 views
1

如何在水豚單元測試中得到詳細的錯誤信息?如何在水豚單元測試中得到詳細的錯誤信息

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. 

回答

3

您可以添加「Customized message」中描述的自定義錯誤消息是這樣的:

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 
+0

由於水豚等行爲,你可能想自定義消息是一個進程因此它的失敗之後,而不是在開始搜索之前評估。 –

+0

優秀的建議@湯姆波爾。我用lambda更新了我的答案。 – MilesStanfield

+0

它顯示錯誤:參數的錯誤數量(1代表0) – riddler0212

0

可以作爲添加自定義錯誤消息如下圖所示,還應該添加屏幕截圖來調試問題。

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 

希望這將幫助:)

相關問題