Here's my code I'm spec'ing:
def vote_up
get_vote
@vote.value += 1 unless @vote.value == 1
@vote.save
respond_to do |format|
format.js { render :action => "vote", :layout => false }
end
end
Seems pretty straightforward. This is what I'm trying to spec it with :
it "should vote up" do
@mock_cat = Factory.create(:category)
Category.stub(:mock_cat)
@mock_post = Factory.create(:post)
Post.stub(:current_post).and_return(@mock_post)
@vote = Factory(:vote)
get :vote_up, :id => @vote
@vote.reload.value.should == 1
end
It's returning this :
undefined method `to_i' for #<Vote:0x1052a4af8>
I can't really figure out why though. If I stubbed my mock_vote as (:vote), wouldn't it run through the controller method and get +1 attributed to it?
Update
Here's the private method from my posts_controller.rb
private
def get_vote
current_post = Post.all.detect{|r| r.id == params[:id].to_i}
@post = current_post
@vote = current_post.votes.find_by_user_id(current_user.id)
unless @vote
@vote = Vote.create(:user_id => current_user.id, :value => 0)
current_post.votes << @vote
end
end
Answer:
it "should vote up" do
@mock_cat = Factory.create(:category)
Category.stub(:mock_cat)
@post = Factory(:post)
get :vote_up, :id => @post.id
@post.reload.vote_score.should == 1
end