Rspec 3.0 is owned by_ from the association

I am trying to check the belongs_to association using rspec 3.0, but I keep working with the error

NoMethodError: undefined the `belongs_to 'method for RSpec :: Core

  it "should belong to a user" do
ride = Ride.new
user = User.new
user.rides << ride
expect(ride).to belong_to user

end

I cannot find anything in the documentation for rspec 3.0 for testing extended associations. Help me please!

+4
source share
3 answers

You can just check the connection:

it "belongs to a user" do
  ride = Ride.new
  user = User.new
  user.rides << ride
  expect(ride.user).to be user
end

In this context, the match bechecks for the identifier of the object. Thus, this will happen if and only if the object userhas the same object_id. In this case, this is what you want, and has semantically significant meaning in how it is read.

+1

expect(ride).to respond_to :user

0

musta-matchers is a gem that provides association, verification, and other comparisons.

Have a look at this answer: Testing associations with rspec-rails 3.0.1 and shoulda does not work

0
source

Source: https://habr.com/ru/post/1546223/


All Articles