So, I'm running Rspec, and trying to figure out why I'm getting this error:
Failure/Error: 3.times {@post.votes.create(value: 1) }
NameError:
uninitialized constant Vote::PostId
Here is my spec/models/post_spec.rb file:
require 'rails_helper'
describe Post do
describe "vote methods" do
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
describe '#up_votes' do
it "counts the number of votes with value = 1" do
expect( @post.up_votes ).to eq(3)
end
end
describe '#down_votes' do
it "counts the number of votes with value = -1" do
expect( @post.down_votes ).to eq(2)
end
end
describe '#points' do
it "returns the sum of all down and up votes" do
expect( @post.points ).to eq(1) # 3 - 2
end
end
end
end
I don't understand why it's giving that line as the error, since its creating data for the Rspec to execute. And when trying to find the "Vote::PostId" in any of my files, it can't be found.
Try using @post.votes.build(value: 1)
Create is used on class. So it can be either Post.create or Vote.create.
In your case, it will try to search for Vote::PostId, such a class does not exist.
Can you find PostId
in your Vote
class? Maybe you were trying to specify a foreign key and typed PostId
rather than "PostId"
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.