간단하고 간단한 것을 간과하면 확실합니다. Factory_Girl이 자동으로 연결을 생성하면 안됩니까? 그렇다면 event_id
이 이 아니기 때문에 "GET 인덱스"사양이 실패한 이유는 무엇입니까??Factory_girl을 통한 연관성 검사에서 RSpec 검증에 실패했습니다.
이벤트 has_many : 게시물
#post.rb
...
belongs_to :event
validates :event_id, :presence => true
...
#factories.rb
Factory.define :event do |event|
event.name "The Long Event Title"
end
Factory.define :post do |post|
post.title "This is a post"
post.association :event
end
#posts_controller_spec.rb
before(:each) do
@attr = Factory.attributes_for(:post)
end
describe "GET index" do
it "assigns all posts as @posts" do
post = @user.posts.create(@attr) ### <-- event_id not assigned?
# post = FactoryGirl.create(:post) ### <-- this doesn't work either
get :index
assigns(:posts).should eq([post])
end
end
편집 : 추가 사양 예 :
describe "POST create" do
describe "with valid params" do
it "creates a new Post" do
expect {
post :create, :post => @attr, :event => Factory.create(:event) <- fail
}.to change(Post, :count).by(1)
end
@attr과 함께 이벤트를 전달하려는 경우 구문은 무엇입니까? * 위의 두번째 스펙 예제를 참조하십시오. * – Meltemi
다음과 같이 할 수 있습니다 :': post => @ attr.merge (: event => Factory.create (: event))'그러면': event' 키에 병합됩니다. 새로 생성 된 Event 객체에)를 @attr 해시에 추가 한 다음 메서드에 전달합니다. – MrDanA
그래, 방금 했어 ... 서투른 느낌이 들지만 ... 괜찮아. thx – Meltemi