Note that there are some explanatory texts on larger screens.

plurals
  1. PORspec test failing for POST member route
    primarykey
    data
    text
    <p>I have a polymorphic vote model which POSTs through a member route on its parent models. Each parent has a "Vote" method in the controller and im trying to test this action in my controller tests. Please view the code below, for simplicity ill show one of the controller actions and test. </p> <p>Regardless of my efforts and trying <code>id: @answer</code>, <code>id: @vote</code>, <code>FactoryGirl.attributes_for(:vote, user_id: @user2)</code> into the expect proc, I can't get this test to pass.</p> <p>factoryGirl.rb</p> <pre><code>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 </code></pre> <p>Answers_controller_spec.rb</p> <pre><code>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 </code></pre> <p>Test failure</p> <pre><code>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 &lt;top (required)&gt;' Finished in 2.01 seconds 9 examples, 1 failure </code></pre> <p>vote.rb</p> <pre><code>class Vote &lt; 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 </code></pre> <p>routes.rb</p> <pre><code> resources :answers do member { post :vote } member { put :correct } resources :comments, except: [:edit, :update] end </code></pre> <p>answers_controller.rb</p> <pre><code> 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload