-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define subject when using Shoulda-Matchers' validate_uniqueness_of #250
Comments
👍 second example looks obviously better. I guess an alternative would be a PR to shoulda that makes this obsolete |
I was under the impression that the second example wouldn't work for uniqueness due to |
@gylaz the reason that the second example works, at least as far as I understand, is that otherwise shoulda uses an implicit subject of In that case, shoulda is not smart enough to use valid attributes for the object, whereas |
Neither of these examples is using an explicit subject. By "explicit subject" the guideline means literally using the method describe "#author_name" do
let(:author) { User.new(name: "Billy") }
subject { Post.new(author: author) }
it "returns its author name" do
expect(subject.author_name).to eq(author.name)
end
end The idea is that "subject" is only useful if it's so obvious which object you're talking about that you don't need to call out the subject. If you need to reference it explicitly, you can use a name instead. If you're using The change linked to for #133 was about Re: |
@jferris I am not sure why Grayson's example with the @Graysonwright do you have more insight into why including the |
Ah, thanks @jferris – I misunderstood. I had assumed that "explicit subject" referred to defining the subject explicitly, not to using it explicitly. That makes a lot more sense now. To confirm, we should be using this syntax, then? describe "Validations" do
subject { build(:registration) }
it { should validate_presence_of(:email) }
it { should validate_presence_of(:mobile_phone_number) }
it { should validate_uniqueness_of(:email) }
it { should validate_uniqueness_of(:mobile_phone_number) }
end |
It looks like |
Yes, that looks correct to me.
Cool; that it explains why |
@Graysonwright are you going to open a PR for this? Would be good to have a comment explaining next steps :) |
require 'rails_helper'
RSpec.describe Score, type: :model do
let(:score) { build(:score) }
subject { described_class }
it { is_expected.to validate_numericality_of(:score) }
it 'belongs to staff' do
assoc = subject.reflect_on_association(:staff)
expect(assoc.macro).to eq :belongs_to
end
end This is my codes, but I occurred the issues, looks like this: » rspec spec/models/score_spec.rb [ruby-2.5.1][19:04:09]
F
Failures:
1) Score should validate that :score looks like a number
Failure/Error: it { is_expected.to validate_numericality_of(:score) }
Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDoesNotExistError:
The matcher attempted to set :score on the Class to "abcd", but that
attribute does not exist. my class Score < ApplicationRecord
belongs_to :staff
validates :score, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
end why? and Thank you very much. |
@TorvaldsDB when you set In Grayson's example above, he sets We frequently use FactoryBot (https://github.com/thoughtbot/factory_bot) in test suits to easily create objects for tests. You might consider giving it a try. |
Can we revisit #133 with respect to thoughtbot/shoulda-matchers#567 ?
I just ran into the Non-NULL database column problem, where
validates_uniqueness_of
didn't know how to create a valid record.Here's what the guides suggested my specs look like:
And here's what it looks like with an explicit subject:
I would suggest that the second option is more readable and more manageable, with less duplication. Thoughts on allowing an explicit subject for use with shoulda-matchers?
The text was updated successfully, but these errors were encountered: