测试友谊:"UserFriendship should belong to friend."错误(轨道 4)



我正在跟踪Treehouse课程,但在那里的论坛上没有积极的回应。这是我遇到的问题:

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)

我"user_friendship_test

require 'test_helper'
class UserFriendshipTest < ActiveSupport::TestCase
  should belong_to(:user)
  should belong_to(:friend)
end
我"user_friendship

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

我应该指出我使用的是Rails 4。

任何帮助都将是非常感激的:)

看起来你没有一个belongs_to正在寻找的Friend类。我怀疑朋友关系也应该是一个User对象,在这种情况下,您需要这样做:

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

如果你使用Spring来运行你的测试(例如bin/rspec),你需要在你的Gemfile中手动要求shouldmatchers

请阅读README获取更多信息:https://github.com/thoughtbot/shoulda-matchers#rspec

最新更新