我是RSpec的新手,我有一个测试场景,我写道:
my_object.should_not be_valid
它运作正常。但是,我想测试模型的特定属性是否无效。是这样一个现成的RSpec行为?我可以这样写:
my_object.should_not be_valid(:name)
理想情况下,我希望能够测试错误的数量,例如:
my_object.should_not be_valid(:name => 1)
但这对我来说并不重要。
答案 0 :(得分:3)
根据this,你应该能够这样写:
describe Person do
it "should validate presence of email" do
person = Person.new(:email =>; nil)
person.should_not be_valid
person.should have(1).error_on(:email)
end
end
或者如下所示,使用these rspec matchers:
describe Person do
it { should validate_presence_of(:email) }
end