加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rspec和FactoryGirl验收验证

发布时间:2020-12-17 04:22:28 所属栏目:百科 来源:网络整理
导读:我一直在试着这个简单的验证,我无法让它验证.我有以下型号: class Attendance ActiveRecord::Base belongs_to :user,counter_cache: true belongs_to :event,counter_cache: true validates :terms_of_service,:acceptance = trueend 这是我的工厂: factor
我一直在试着这个简单的验证,我无法让它验证.我有以下型号:
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,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映射到db列? validates的默认值:acceptance是字符串“1”,不是true.
如果它映射到db列,请尝试添加:accept =>验证是真的:
validates :terms_of_service,:acceptance => {:accept => true}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读