Skip to content
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

[Question] Working with belongs_to :relation, required: true #861

Closed
rennanoliveira opened this issue Dec 11, 2015 · 6 comments
Closed

[Question] Working with belongs_to :relation, required: true #861

rennanoliveira opened this issue Dec 11, 2015 · 6 comments

Comments

@rennanoliveira
Copy link

If you have something like

class Group < ActiveRecord::Base
  belongs_to :manager, required: true
end

Currently, what I can do on my specs is:

it { is_expected.to belong_to(:manager) }
it { is_expected.to validate_presence_of(:manager) }

Which does cover it for me, but I'm curious as to ideas on having something in the likes of:

it { is_expected.to belong_to(:manager).with_required(true) }

If it's in the docs, I apologize, I couldn't find it

@mcmire
Copy link
Collaborator

mcmire commented Dec 14, 2015

I believe this may be a newer option that we haven't added yet (I haven't heard of it).

That would be a good idea to add (although I'd have the qualifier be required(true) instead of with_required(true)).

@ojab
Copy link
Contributor

ojab commented Jan 12, 2016

In rails 5 required: true will be default option for belongs_to, so it makes sense to make it default in shoulda-matchers too.
See also #870.

@zavan
Copy link

zavan commented Feb 27, 2016

I'm using Rails 5.0.0.beta3 with RSpec 3.5.0.beta2 and shoulda-matchers 3.1.1

Consider:

class User < ApplicationRecord
  has_many :changesets, inverse_of: :user, dependent: :restrict_with_error
end

class Changeset < ApplicationRecord
  belongs_to :user, inverse_of: :changesets
end

RSpec.describe Changeset, type: :model do
  subject { create :changeset }

  it { is_expected.to belong_to(:user).inverse_of(:changesets) }
  it { is_expected.to validate_presence_of :user }
end

Rails changed the "required" belongs_to validation message.
The it { is_expected.to validate_presence_of :user } assertion fails:

  1) Changeset should validate that :user cannot be empty/falsy
     Failure/Error: it { is_expected.to validate_presence_of :user }

       Changeset did not properly validate that :user cannot be empty/falsy.
         After setting :user to ‹nil›, the matcher expected the Changeset to be
         invalid and to produce the validation error "can't be blank" on :user.
         The record was indeed invalid, but it produced these validation errors
         instead:

         * user: ["must exist"]

Specifying the message fixes it for now:

it { is_expected.to validate_presence_of(:user).with_message('must exist') }

@murbanski
Copy link

Just FYI, try .with_message(:required) to shorten it a little bit:)

pikender pushed a commit to vinsol-spree-contrib/spree_favorite_products that referenced this issue Feb 8, 2017
Fixed deprecation warning uniq -> distinct
See: rails/rails#20198
Fixed exception for validate_presence “The record was indeed invalid, but it produced these validation errors instead”
See: thoughtbot/shoulda-matchers#861 (comment)
Fixed deprecation warning - use keyword syntax for get, put etc in controller tests
Fixed coffee-rails not loading error
@mcmire mcmire added this to the v4.0 milestone Sep 19, 2017
@robbl-as
Copy link

robbl-as commented Jan 8, 2018

@mcmire @stephanngamedev Isn't that issue resolved by now?

@mcmire
Copy link
Collaborator

mcmire commented Jan 24, 2018

@robbl-as Yup! Sorry. Not sure why this issue wasn't closed automatically. This will be in the next release, or if you're feeling adventurous, you can point to master.

@mcmire mcmire closed this as completed Jan 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants