2013-06-19 41 views
0

我有一個多態的投票模型,通過其父模型成員路線POST。每個父母在控制器中都有一個「投票」方法,我試圖在我的控制器測試中測試這個動作。請查看下面的代碼,爲了簡單起見,請顯示控制器操作和測試之一。Rspec測試失敗POST成員路線

無論我的努力和嘗試id: @answer,id: @vote,FactoryGirl.attributes_for(:vote, user_id: @user2)進入期望過程,我無法通過此測試。

factoryGirl.rb

FactoryGirl.define do 
    factory :user do |u| 
    u.sequence(:email) {|n| "test#{n}@hotmail.com"} 
    u.sequence(:username) {|n| "tester#{n}" } 
    u.password "password" 
    u.password_confirmation "password" 
    u.remember_me true 
    u.reputation 200 
    end 

factory :answer do 
    user_id :user 
    question_id :question 
    body "you need to change your grip" 
    votes_count 0 
    correct true 
    end 

factory :vote do 
    user_id :user 
    votable_id :answer 
    votable_type "Answer" 
    value 1 
    end 

Answers_controller_spec.rb

describe "POST vote" do 
    it "creates vote" do 
     @user2 = FactoryGirl.create(:user) 
     @answer = FactoryGirl.create(:answer) 
     @vote = FactoryGirl.create(:vote) 
     expect { 
     post :vote, id: @vote 
     }.to change(Vote, :count).by(1) 
     response.should be_success 
    end 
    end 

測試失敗

Failures: 

    1) AnswersController POST vote creates vote 
    Failure/Error: expect { 
     count should have been changed by 1, but was changed by 0 
    # ./spec/controllers/answers_controller_spec.rb:90:in `block (3 levels) in <top (required)>' 

Finished in 2.01 seconds 
9 examples, 1 failure 

vote.rb

class Vote < ActiveRecord::Base 

    attr_accessible :value, :votable_id, :votable_type 
    belongs_to :votable, polymorphic: true 
    belongs_to :user 

    validates_inclusion_of :value, in: [1, -1] 
    validates_presence_of :user_id, :value, :votable_id, :votable_type, :points 
    validates_uniqueness_of :user_id, scope: :votable_id 
    validates_uniqueness_of :value, scope: :votable_id 

    before_save :create_points 

    def create_points 
    self.value == 1 ? self.points = 5 : self.points = -3 
    end 
end 

的routes.rb

resources :answers do 
    member { post :vote } 
    member { put :correct } 
    resources :comments, except: [:edit, :update] 
    end 

answers_controller.rb

def vote 
    @vote = current_user.votes.build(value: params[:value], votable_id: params[:id], votable_type: "Answer") 
    respond_to do |format| 
    if @vote.save 
     format.html { redirect_to :back, notice: "Vote submitted" } 
     format.js 
    else 
     format.html { redirect_to :back, alert: "You can't vote on your own content" } 
     format.js 
    end 
    end 
end 

回答

1

id參數是答案的ID:

post :vote, id: @answer.id 

您可能還需要通過value參數(也許'up'或'down'值?)。

測試失敗可能有其他原因,例如, Vote模型中的驗證。如果vote.save返回false,則檢查errors集合。

以下意見:

您需要將value傳遞的參數去post在你的天賦。它也似乎你有一個重複的投票,你可能不需要在你的規範中創建。該errors集合由valid?方法填充,該方法由save調用。

+0

我認爲它是一個驗證問題,在註釋驗證時,測試通過,FactoryGirl對象有問題嗎? – dodgerogers747

+1

如果工廠有問題,我可能看不到工廠有什麼問題(未顯示)。如果我想迅速弄清楚出了什麼問題,我會在'else'後面插入'raise @ vote.errors.inspect'(快速查看錯誤)或'binding.pry'(調試)到控制器中。並重新運行測試。 – Steve

+0

我已將答案因子添加到原始問題中。 – dodgerogers747