Shoulda/RSpec: Make sure that validation message “xxx” is on :base

*爱你&永不变心* 提交于 2019-12-06 16:00:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!