所以,我运行Rspec的,并试图找出为什么我收到此错误:NameError:在Rspec的未初始化不断
Failure/Error: 3.times {@post.votes.create(value: 1) }
NameError:
uninitialized constant Vote::PostId
这里是我的规格/型号/ post_spec.rb文件:
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
我不明白为什么它将该行作为错误,因为它为Rspec执行创建数据。当试图在我的任何文件中找到“Vote :: PostId”时,找不到它。
'@ post.votes'返回可以调用'create'的'ActiveRecord :: Relation'实例,所以'@ post.votes.create(value:1)'是合法的。 – Ben 2014-09-30 05:16:32