2014-09-30 2 views
0

그래서, 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"를 찾으려고 할 때 찾을 수 없습니다.

답변

0

시도 @의 post.votes.build를 사용하여 (값 : 1)

작성이 클래스에 사용됩니다. 따라서 Post.create 또는 Vote.create가 될 수 있습니다.

귀하의 경우, 투표 :: PostId를 검색하려고 시도하지만 해당 클래스는 존재하지 않습니다.

+0

'@ post.votes'는'create'가 호출 될 수있는'ActiveRecord :: Relation'의 인스턴스를 반환하므로'@ post.votes.create (value : 1)'은 합법입니다. – Ben

0

Vote 클래스에서 PostId을 찾을 수 있습니까? 외부 키를 지정하려고 시도하고 "PostId" 대신 PostId을 입력 했습니까?

관련 문제