2016-05-03 12 views
1
のユーザーを見つけることができません

リレーションシップコントローラーの仕様を作成しようとしていて、#createメソッドでエラーが発生しました( 'id' = )。 これを修正する方法を教えていただけますか? ご質問ありがとうございましたらお気軽にお問い合わせください。 :)リレーションシップコントローラーのRspecエラー 'id' =

relationships_controller:

class RelationshipsController < InheritedResources::Base 

    def create 
    user = User.find(params[:followed_id]) 
    current_user.follow(user) 
    redirect_to user 
    end 

    def destroy 
    user = Relationship.find(params[:id]).followed 
    current_user.unfollow(user) 
    redirect_to user 
    end 
end 

relationships_controller_spec:

require 'rails_helper' 

describe RelationshipsController do 
    let(:relationship) { create(:relationship) } 
    let(:user) { create(:user) } 

    before do 
    sign_in :user, create(:user) 
    end 


    describe '#create' do 
    let!(:user) { create(:user) } 
    it "should require logged-in user to create relationship" do 
     post :create, followed_id: user.id 
     expect{ 
     Relationship.create 
     }.to change(Relationship, :count).by(1) 
     redirect_to root_path 
    end 
    end 


    describe '#create' do 
    let!(:relationship) { create(:relationship) } 

    it "should require logged-in user to destroy relationship" do 
     expect { 
     delete :destroy, id: relationship.id 
     }.to change(Relationship, :count).by(-1) 
     redirect_to root_path 
    end 
    end 
end 

失敗:

RelationshipsController 
    #create 
    should require logged-in user to destroy relationship 
    #create 
    should require logged-in user to create relationship (FAILED - 1) 

Failures: 

    1) RelationshipsController#create should require logged-in user to create relationship 
    Failure/Error: 
     expect{ 
     Relationship.create 
     }.to change(Relationship, :count).by(1) 

     expected #count to have changed by 1, but was changed by 0 

関係:

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

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

ユーザー:あなたのテストで

class User < ActiveRecord::Base 
     # Associations 
     has_many :active_relationships, class_name: "Relationship", 
             foreign_key: "follower_id", 
             dependent: :destroy 
     has_many :passive_relationships, class_name: "Relationship", 
             foreign_key: "followed_id", 
             dependent: :destroy 
     has_many :following, through: :active_relationships, source: :followed 
     has_many :followers, through: :passive_relationships, source: :follower 

     # Follows a user. 
     def follow(other_user) 
     active_relationships.create(followed_id: other_user.id) 
     end 
    end 
+0

'follows_id'パラメータ –

+0

あなたのコントローラスペックでは、投稿要求を行うときに意味します –

+0

また、あなたの期待は手動で関係thを作成しているようですカウントをチェックする。これは実際にコントローラをテストするものではありません。 –

答えて

0

エンド:あなたが投稿されていませんしました

describe '#create' do 
    let!(:followed) { create(:user) } 
    it "should require logged-in user to create relationship" do 
     expect{ 
     post :create, followed_id: followed.id 
     }.to change(Relationship, :count).by(1) 
     redirect_to root_path 
    end 
    end 
1

-

describe RelationshipsController do 
    let(:relationship) { create(:relationship) } 
    let(:user) { create(:user) } 

ユーザーが必要になるまで作成されていません。

describe RelationshipsController do 
    let(:relationship) { create(:relationship) } 
    let!(:user) { create(:user) } 

!演算子は、ユーザー変数を作成してデータベースに格納するように強制します。それ以外の場合、変数は遅延ロードされます(必要な場合のみ)。それは仕様でデータベース

2

パス:followed_idでは使用できず、再びそれを実行されている理由です

describe '#create' do 
it "should require logged-in user to create relationship" do 
    post :create, followed_id: user.id 
    expect{ 
    Relationship.create 
    }.to change(Relationship, :count).by(1) 
    redirect_to root_path 
end 

これが働い

+0

更新された質問 – Jony

関連する問題