gpt4 book ai didi

ruby-on-rails - Shoulda 和 RSpec 之前

转载 作者:行者123 更新时间:2023-11-28 20:24:18 25 4
gpt4 key购买 nike

在测试模型字段的有效性之前,我尝试在主题中设置一个实例变量。我需要设置这个变量,因为验证是有条件的(它只用于某些类型的用户)。所以我有这样的东西:

  context "as a user" do

before(:each) do
subject = Organization.new
subject.editor = "user"
end

it { subject.should validate_presence_of :name }

end

但它并没有像预期的那样工作:

 Failure/Error: it { subject.should validate_presence_of :description }
RuntimeError:
Organization#editor attr is not set

我错过了什么?

最佳答案

subject 在你的 before block 中是一个局部变量。看起来你打算使用 an explicit subject :

context "as a user" do
subject { Organization.new }

before(:each) do
subject.editor = "user"
end

# usually, you don't explicitly name the subject in an `it` like this
it { should validate_presence_of :name }

end

关于ruby-on-rails - Shoulda 和 RSpec 之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16039559/

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