第11章のこのメソッド「be_following」はどこから来たのだろうか? user_spec のスニペットは次のとおりです。
describe "following" do
it { should be_following(other_user) }
its(:followed_users) { should include(other_user) }
describe "followed user" do
subject { other_user }
its(:followers) { should include(@user) }
end
end
このメソッドの作成方法がわかりません。私の知る限り、これはデフォルトの rspec またはカピバラのメソッドではありません。has_manyやbelongs_toなどのモデル関係を定義するときに、rspecでRailsによって生成されたメソッドを使用できるかどうかさえわかりません。そうですか?そして、この場合、モデルでさえ定義されていないのに、be_following メソッドがあるのはなぜですか。ユーザーモデルは次のとおりです。
has_many :years
has_many :relationships, dependent: :destroy, foreign_key: :follower_id
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
def following?(followed)
relationships.find_by_followed_id(followed)
end
def follow!(followed)
relationships.create!(:followed_id => followed.id)
end
def unfollow!(other_user)
relationships.find_by(followed_id: other_user.id).destroy!
end