【问题标题】:shoulda-matchers attribute does not seem to be an attribute on modelshoulda-matchers 属性似乎不是模型上的属性
【发布时间】:2018-04-12 06:31:01
【问题描述】:

我刚刚在 Rails 4.1 上从 2.8.0 升级到 shoulda-matchers 3.1.2,现在我的 validate uniqueness scoped to test 失败并显示此消息;

  1) Invoice should validate that :invoice_no is case-sensitively unique within the scope of :ledger_id
     Failure/Error: it { is_expected.to validate_uniqueness_of(:invoice_no).scoped_to(:ledger_id)}

       Invoice did not properly validate that :invoice_no is case-sensitively
       unique within the scope of :ledger_id.
         :ledger_id does not seem to be an attribute on Invoice.
     # ./spec/models/invoice_spec.rb:6:in `block (2 levels) in <top (required)>'

这是我的测试

require 'rails_helper'

RSpec.describe Invoice, :type => :model do
  it { is_expected.to validate_uniqueness_of(:invoice_no).scoped_to(:ledger_id)}
  it { is_expected.to validate_presence_of(:ledger_id)}

是否存在 :ledger_id 的测试通过。

这是我的模型

  validates :ledger_id, presence: true
  validates_uniqueness_of :invoice_no, scope: :ledger_id, case_sensitive: false

解决方案(我还在用机械师)

  subject { Invoice.make!(ledger: Ledger.make!) }
  it { is_expected.to validate_uniqueness_of(:invoice_no).case_insensitive.scoped_to(:ledger_id)}

【问题讨论】:

    标签: ruby-on-rails shoulda


    【解决方案1】:

    我认为你的问题thoughtbot/shoulda-matchers

    试试这个:

    require 'rails_helper'
    
    RSpec.describe Invoice, :type => :model do
     is_expected = expect(create(:invoice))
     it { is_expected.to 
     validate_uniqueness_of(:invoice_no).case_insensitive.scoped_to(:ledger_id)}
     it { is_expected.to validate_presence_of(:ledger_id)}
    

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-06-20
      • 2010-11-16
      • 1970-01-01
      • 2011-06-08
      • 2010-10-08
      • 1970-01-01
      相关资源
      最近更新 更多