Reputation: 5126
I've just upgraded to shoulda-matchers 3.1.2 from 2.8.0 on Rails 4.1 and now my validate uniqueness scoped to test fails with this message;
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)>'
Here is my test
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)}
The test for the presence of :ledger_id passes.
Here is my model
validates :ledger_id, presence: true
validates_uniqueness_of :invoice_no, scope: :ledger_id, case_sensitive: false
Solution (I'm using machinist still)
subject { Invoice.make!(ledger: Ledger.make!) }
it { is_expected.to validate_uniqueness_of(:invoice_no).case_insensitive.scoped_to(:ledger_id)}
Upvotes: 0
Views: 844
Reputation: 679
I think there is your issue thoughtbot/shoulda-matchers
Try this:
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)}
Upvotes: 1