2016-10-30 10 views
0

ユーザーが特定の種類の食べ物に投票したときに、user.votesの数とfood.votesの両方が上昇するようにアプリをテストしようとしています。現時点では、私はそれが起こることはできません、私は間違ったテストを書いたか、テストが合格できない工場を設立したのかどうかはわかりません。それは私がコンソールで手動でテストするときに機能するので、機能することは分かっていますが、テストには同意しません。推測を取るのに十分な自信を持って[OK]を1つの工場のデータを別の工場で使用する方法

FactoryGirl.define do 
factory :vote do |f| 
    f.user_id user 
    f.food_id food 
end 
end 

def create 
@vote = Vote.new(vote_params) 

if @vote.save 
    flash[:notice] = "Thanks for voting!" 
else 
    flash[:notice] = "Something went wrong" 
end 
end 

before(:each) do 
    @user = create(:user) 
    sign_in(@user) 
    @food = create(:food) 
end 

describe "POST #create" do 
context "with valid attributes" do 
    it "creates a new vote" do 
    vote_params = FactoryGirl.attributes_for(:vote) 
    expect { post :create, params: { vote: vote_params } }.to change(Vote, :count).by(1) 
    end 

    it "Updates @user vote count" do 
    vote_params = FactoryGirl.attributes_for(:vote) 
    expect { post :create, params: { vote: vote_params } }.to change(@user.votes, :count).by(1) 
    end 

    it "Updates @food vote count" do 
    vote_params = FactoryGirl.attributes_for(:vote) 
    expect { post :create, params: { vote: vote_params } }.to change(@food.votes, :count).by(1) 
    end 
end 
end 

Failures: 

1) VotesController POST #create with valid attributes Updates @user vote count 
Failure/Error: expect { post :create, params: { vote: vote_params } }.to change(@user.votes, :count).by(1) 
    expected #count to have changed by 1, but was changed by 0 
# ./spec/controllers/votes_controller_spec.rb:27:in `block (4 levels) in <top (required)>' 

2) VotesController POST #create with valid attributes Updates @food vote count 
Failure/Error: expect { post :create, params: { vote: vote_params } }.to change(@food.votes, :count).by(1) 
    expected #count to have changed by 1, but was changed by 0 
# ./spec/controllers/votes_controller_spec.rb:32:in `block (4 levels) in <top (required)>' 

Finished in 2.99 seconds (files took 4.66 seconds to load) 
36 examples, 2 failures 

Failed examples: 

rspec ./spec/controllers/votes_controller_spec.rb:25 # VotesController POST  #create with valid attributes Updates @user vote count 
rspec ./spec/controllers/votes_controller_spec.rb:30 # VotesController POST #create with valid attributes Updates @food vote count 
+0

がもっともらしくカウントが更新するためにあなたが '@のuser.reload'が必要になることだろう...あなたがテストすることができ、この場合{Vote.where(:user_id => @ user.id).count} .by(1) 'を変更するには、次のようなものを試してみてください。あなたの提案に常に新鮮なデータ –

+0

を持っていることを確認してください。私はそれを試しましたが、残念ながらそれはまだ0から1に変更されていないと言っています –

+0

ok ...そうではありません。投票に奇妙な検証エラーがある、またはユーザーが適切にサインインしていないか、そんなばかげたことが起こっている可能性があります。あなたはそれらのもののそれぞれをチェックできますか? ''新しい投票を作成する ''合格か、それともすべて失敗するのでしょうか?障害のエラーメッセージを表示できますか? (コメントではなくオリジナルの質問をintに入れて、コードフォーマットがあまりにも恐ろしいものではないようにしてください):) –

答えて

0

、。問題は、vote_paramsが工場から来ていることです(実際にはuser_id/food_idの値はハードコードされている可能性があります)が、テスト対象の実際の@user@foodはありません。あなたのvote_paramsにこれらの値を配置する必要があり、私は次のようなものを提案することができます:

context "with valid attributes" do 
    # Set vote-params from factory, then merge in the @user/@food ids 
    let(:vote_params) { FactoryGirl.attributes_for(:vote).merge(:user_id => @user.id, :vote_id => @vote.id) } 

    it "creates a new vote" do 
    expect { post :create, params: { vote: vote_params } }.to change(Vote, :count).by(1) 
    end 

    it "Updates @user vote count" do 
    expect { post :create, params: { vote: vote_params } }.to change(@user.votes, :count).by(1) 
    end 

    it "Updates @food vote count" do 
    expect { post :create, params: { vote: vote_params } }.to change(@food.votes, :count).by(1) 
    end 
end 
+0

私はこれを試してみると大丈夫です:NoMethodError: 'id 'for nil:NilClass –

+1

大丈夫私はそれを渡すようになった!私はあなたの提案を使用しましたが、.mergeの部分を(:user_id => @user、:food_id => @food)に変更しなければなりませんでした。 –

関連する問題