问题
I'm still pretty confused about what is magic behind stuff like it { should have(1).error_on(:base) }
and what's a specific Shoulda matcher.
I'd like to make sure that :base
contains the error message "xxx", so how should I do this?
it "should contain error message 'xxx'" do
contact.valid?
contact.errors[:base].should include('xxx')
end
Is this "the way to go", or is there a better one? Thanks.
回答1:
Right, it's looking good. Inline rspec tests are using subject. You could rewrite your test like this:
describe 'my method' do
before { contact.valid? }
context 'contact is not valid' do
subject { contact.errors[:base] }
it { should include 'xxx' }
end
end
The should method is called on the subject. It can be more readable sometimes. And you don't have to write descriptions for specs that are self-explanatory ;-)
来源:https://stackoverflow.com/questions/11453152/shoulda-rspec-make-sure-that-validation-message-xxx-is-on-base