2011-03-11 54 views

回答

0

不知道如果這是你在尋找什麼,但你可以在saveupdate

@widget.save 
#untested, but this should be close 
@widget.errors.full_messages.include?("validation message you are looking for").should be true 

後檢查錯誤信息但說實話,這可能不是你需要在你的單元測試來測試(如果這是你在哪裏放置它們)的東西。你基本上覆制了Rails已經爲你完成的單元測試。在黃瓜集成測試中檢查視圖中的錯誤消息會更合適。

6
before(:each) do 
    @attr = { :bar => "foobar" } 
end 

it "should reject duplicate bar" do 
    Foo.create!(@attr) 
    duplicate_bar = Foo.new(@attr) 
    duplicate_bar.should_not be_valid 
end