gpt4 book ai didi

rspec - 使用 RSpec,有没有办法断言模型的特定属性是否无效?

转载 作者:行者123 更新时间:2023-12-04 05:50:49 26 4
gpt4 key购买 nike

我是 RSpec 的新手,我在其中编写了一个测试场景:

my_object.should_not be_valid

它工作正常。但是,我想测试模型的特定属性是否无效。这样的 RSpec 行为是现成的吗?我可以写一些类似的东西:
my_object.should_not be_valid(:name)

理想情况下,我也希望能够测试错误数量,例如:
my_object.should_not be_valid(:name => 1)

但现在这对我来说并不重要。

最佳答案

根据 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

关于rspec - 使用 RSpec,有没有办法断言模型的特定属性是否无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10072711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com