应该/RSpec:确保验证消息“xxx"在 :base [英] Shoulda/RSpec: Make sure that validation message "xxx" is on :base
问题描述
我仍然很困惑it { should have(1).error_on(:base) }
之类的东西背后的魔法是什么,以及什么是特定的 Shoulda 匹配器.
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.
我想确保 :base
包含错误消息xxx",那么我该怎么做?
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.
推荐答案
好吧,看起来不错.内联 rspec 测试使用主题.你可以像这样重写你的测试:
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
在主题上调用 should 方法.有时它可能更具可读性.而且您不必为不言自明的规格编写描述;-)
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 ;-)
这篇关于应该/RSpec:确保验证消息“xxx"在 :base的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!