Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Treebook adding friendships

I am going through testing friendships using shoulda tutorial link: http://teamtreehouse.com/library/programming/building-social-features-in-ruby-on-rails/creating-friendships/testing-friendships

My test fails giving me this error

  1) Failure:
test: UserFriendship should belong to friend. (UserFriendshipTest) [C:/RailsInst
aller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/shoulda-context-1.1.5/lib/shoulda/conte
xt/context.rb:339]:
Expected UserFriendship to have a belongs_to association called friend (UserFrie
ndship does not have a friend_id foreign key.)

  2) Failure:
test: UserFriendship should belong to user. (UserFriendshipTest) [C:/RailsInstal
ler/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/shoulda-context-1.1.5/lib/shoulda/context
/context.rb:339]:
Expected UserFriendship to have a belongs_to association called user (UserFriend
ship does not have a user_id foreign key.)

I have tried both the suggested methods in other discussion threads 1) Adding following in gem file

group :test do
  gem 'shoulda'
  gem 'shoulda-matchers'
  gem 'shoulda-context'
end

2) Adding following in test_helper.rb

include Shoulda::Matchers::ActiveRecord
extend Shoulda::Matchers::ActiveRecord
include Shoulda::Matchers::ActiveModel
extend Shoulda::Matchers::ActiveModel

I am still having the error mentioned above. My user_friendship_test looks like

require 'test_helper'

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

and user_friendship.rb

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

Any help will be appreciated.

1 Answer

Hmmm....