Question

I'm following a Treehouse course, but have had no positive response on the forums there. Here's the issue I'm having:

1) Failure: UserFriendshipTest#test_: UserFriendship should belong to friend. [/Users/Sam/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:344]: Expected UserFriendship to have a belongs_to association called friend (Friend does not exist)

My 'user_friendship_test.rb' file looks like this:

require 'test_helper'

class UserFriendshipTest < ActiveSupport::TestCase
  should belong_to(:user)
  should belong_to(:friend)
end

My 'user_friendship.rb' file looks like this:

class UserFriendship < ActiveRecord::Base
   belongs_to :user
   belongs_to :friend
end

I should point out that I'm using Rails 4.

Any help would be much appreciated :)

Was it helpful?

Solution

It looks like you don't have a Friend class which the belongs_to is looking for. I suspect that the friend relation is also a supposed to be a User object, in which case you need something like this:

class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: "User"
end

OTHER TIPS

If you're using Spring to run your tests (i.e. bin/rspec), you'll need to manually require shoulda-matchers in your Gemfile.

Please read the README for more information: https://github.com/thoughtbot/shoulda-matchers#rspec

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top