ror断言失败,没有给出消息

时间:2015-12-13 16:28:44

标签: ruby-on-rails ruby-on-rails-3

我是ROR新手,我跟着ch12,这是我的user_test.rb

  test "should follow and unfollow a user" do
     user_test = users(:test)
     ancher = users(:ancher)
     assert_not user_test.following?(ancher)
     user_test.follow(ancher)
     assert user_test.following?(ancher) ------> error line 82
     user_test.unfollow(ancher)
     assert_not user_test.following?(ancher)
  end

这是我的user.rb

class User < ActiveRecord::Base
    has_many :microposts, dependent: :destroy

    has_many :active_relationships, class_name: "Relationship",
                                      foreign_key: "followed_id",
                                      dependent: :destroy

    has_many :following, through: :active_relationships, source: :followed

    ... 

    def follow(other_user)
        active_relationships.create(followed_id: other_user.id)
    end

    def unfollow(other_user)
        active_relationships.find_by(followed_id: other_user.id).destroy
    end

    def following?(other_user)
        following.include?(other_user)
    end

    ...

这是我的relationship.rb

class Relationship < ActiveRecord::Base
    belongs_to :follower, class_name: "User" 
    belongs_to :followed, class_name: "User"

    validates :follower_id, presence: true
    validates :followed_id, presence: true
end

当我运行bundle exec rake test时。我总是得到

 FAIL["test_should_follow_and_unfollow_a_user", UserTest, 2015-12-09 17:32:33 +0000]
 test_should_follow_and_unfollow_a_user#UserTest (1449682353.62s)
        Failed assertion, no message given.
        test/models/user_test.rb:82:in `block in <class:UserTest>'

我不知道,谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。这是我的用户关系。

has_many :active_relationships, class_name: "Relationship",
                                  foreign_key: "follower_id", ---> error
                                  dependent: :destroy