Reputation: 605
My rspec test vote__spec.rb is stating that my up_votes method is undefined but it is listed within the votes.rb file listed below. Can anyone guide me on why its stating it is undefined?
Rspec error
2) Vote validations value validation only allows -1 or 1 as values
Failure/Error: expect(@votes.up_votes).where(value: [1,-1])
NoMethodError:
undefined method `up_votes' for nil:NilClass
# ./spec/models/vote_spec.rb:5:in `block (4 levels) in <top (required)>'
Vote Rspec
describe Vote do
describe "validations" do
describe "value validation" do
it "only allows -1 or 1 as values" do
expect(@post.up_votes).where(value: [1,-1])
end
end
end
end
Vote.rb file
class Vote < ActiveRecord::Base
belongs_to :user
belongs_to :post
def up_votes
votes.where(value: 1).count
end
end
Upvotes: 0
Views: 51
Reputation: 605
The error is appearing because the @post variable needs to be initialized. Once I initialized the @post variable as shown below, the error is resolved:
before do
@post = Post.create(title: 'Post title', body: 'Post bodies must be pretty long.')
3.times {@post.votes.create(value: 1)}
2.times {@post.votes.create(value: -1)}
end
Upvotes: 1