How does one do this? Couldn't find any examp开发者_如何学编程les online... (using rspec 2.5.0
& rails 3.0.5
)
Found it in shoulda-matchers
: http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames
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
Not sure if this exactly what you are looking for, but you could check the error messages after the save
or update
@widget.save
#untested, but this should be close
@widget.errors.full_messages.include?("validation message you are looking for").should be true
But honestly, this is probably not something that you need to test in your unit tests (if that is where you are placing them). You are basically duplicating unit tests that Rails has already done for you. It would be more appropriate to check for the error message in the view in a cucumber integration test.
精彩评论