2016-12-21 1 views
1

학교에서 앱을 제작 중이며이 오류가 발생합니다. 현 시점에서 app walk through는 4.2.6 레일에서 시작되었으며 5.0.0.1을 실행 중입니다.레일즈 5에서 레코드 생성과 함께 테스트가 실패했습니다.

오류는 다음과 같이

Failures: 

    1) Post Creation can be created 
    Failure/Error: expect(@post).to be_valid 
    expected #<Post id: nil, date: "2016-12-20", rationale: "Anything", created_at: nil, updated_at: nil, user_id: nil> to be valid, but got errors: User must exist 
    # ./spec/models/post_spec.rb:10:in `block (3 levels) in <top (required)>' 

Finished in 0.65569 seconds (files took 2.19 seconds to load) 
10 examples, 1 failure 

Failed examples: 

    rspec ./spec/models/post_spec.rb:9 # Post Creation can be created 

내 코드입니다. 나는 walk-through에 repo에 비교하고 완벽하게 일치합니다. 나는 무엇을 놓치고 있습니까?

require 'rails_helper' 

RSpec.describe Post, type: :model do 
    describe "Creation" do 
    before do 
     @post = Post.create(date: Date.today, rationale: "Anything") 
    end 

    it "can be created" do 
     expect(@post).to be_valid 
    end 

    it "cannot be created without a date and rationale" do 
     @post.date = nil 
     @post.rationale = nil 
     expect(@post).to_not be_valid 
    end 
    end 
end 
+0

'test.log' 파일을 확인하십시오. 레코드가 어떤 이유로 데이터베이스에 저장되지 않았고 로그를 조사하지 않고 정확히 이유를 말할 수 없습니다. – mudasobwa

답변

0

레일 (5) 당신이 belongs_to 관계가있을 때, 그에서 레일 4 다르더라도 당신이 어떤 검증을 추가하지 않고, 연관된 객체의 5 것이다 automatically validate the presence 레일.

아마도 Post 모델의 모델은 User입니다. 이 때문에 테스트 설정에서 사용자를 만들어야합니다. 그렇지 않으면 유효성 검사가 실패합니다.

describe "Creation" do 
    before do 
    @user = User.create(...) 
    @post = Post.create(date: Date.today, rationale: "Anything", user: @user) 
    end 

    it "can be created" do 
    expect(@post).to be_valid 
    end 

    it "cannot be created without a date and rationale" do 
    @post.date = nil 
    @post.rationale = nil 
    expect(@post).to_not be_valid 
    end 
end 
관련 문제