gpt4 book ai didi

ruby-on-rails - Rspec & FactoryGirl 验收验证

转载 作者:行者123 更新时间:2023-12-04 10:54:46 25 4
gpt4 key购买 nike

我一直在为这个简单的验证而头疼,但我无法对其进行验证。我有以下模型:

class Attendance < ActiveRecord::Base
belongs_to :user, counter_cache: true
belongs_to :event, counter_cache: true

validates :terms_of_service, :acceptance => true
end

这是我的工厂:
  factory :attendance do
user
event
terms_of_service true
end

这是我的测试:
  describe "event has many attendances" do
it "should have attendances" do
event = FactoryGirl.create(:event)
user1 = FactoryGirl.create(:user, firstname: "user1", email: "mail@user2.nl")
user2 = FactoryGirl.create(:user, firstname: "user2", email: "mail@user1.nl")

attendance1 = FactoryGirl.create(:attendance, event: event, user: user1, terms_of_service: true)
end
end

这不应该带来任何错误,但确实如此。
Running spec/models/workshop_spec.rb
.............F

Failures:

1) Event event has many attendances should have attendances
Failure/Error: attendance1 = FactoryGirl.create(:attendance, event: event, user: user1, terms_of_service: true)
ActiveRecord::RecordInvalid:
Validation failed: Terms of service must be accepted
# ./spec/models/event_spec.rb:33:in `block (3 levels) in <top (required)>'

当我在浏览器中执行这些操作并接受这些操作时,一切顺利。我在这里错过了什么?!

最佳答案

:terms_of_service映射到数据库列? validates :acceptance 的默认值是字符串“1”,而不是 true .
如果映射到db列,尝试添加:accept => true验证:

validates :terms_of_service, :acceptance => {:accept => true}

如果该字段未映射,或者 DB 列不是 bool 值,请尝试在测试和工厂中使用“1”而不是 true。

关于ruby-on-rails - Rspec & FactoryGirl 验收验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11791881/

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