2013-10-28 7 views
2

많은 활동에 속한 활동이 있습니다. rSpec으로 공장을 테스트 할 때마다 오류가 발생합니다.FactoryGirl과의 연관성이 정의되지 않은 메소드를 던졌습니다.

it "has valid factory" do 
    expect(FactoryGirl.build(:activity)).to be_valid 
end 

그리고 주어진 오류는 다음과 같습니다 :

테스트이다

1) Activity has valid factory 
    Failure/Error: expect(FactoryGirl.build(:activity)).to be_valid 
    NoMethodError: 
     undefined method `client=' for #<Activity:0x6cbb800> 
    # C:in `object' 
    # ./spec/models/activity_spec.rb:5:in `block (2 levels) in <top (required)> 

활동 공장association :client를 추가 한 이후이 같은되었습니다 :

FactoryGirl.define do 
    factory :activity do 
     title "Activity" 
     start_date {Date.parse('2013-05-13 12:00:00')} 
     end_date {Date.parse('2013-05-13 12:15:00')} 
     pictogram_id "1" 
     association :client 
    end 
end 

그리고 클라이언트 자체가 모든 테스트를 통과합니다. 같은 공장 : (clients.rb)

FactoryGirl.define do 
    factory :client do 
     name "Client name" 
     background "#FFFFFF" 
     birthdate {15.years.ago} 
    end 
end 

그것이 내가 내 schema.rb의 관련 부분 포스트를 업데이트하고있어 테스트 DB 준비/복제 문제가 아니에요 때문에

create_table "activities", force: true do |t| 
    t.string "title" 
    t.datetime "start_date" 
    t.datetime "end_date" 
    t.integer "pictogram_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "activities", ["pictogram_id"], name: "index_activities_on_pictogram_id" 

    create_table "activities_clients", id: false, force: true do |t| 
    t.integer "activity_id" 
    t.integer "client_id" 
    end 

    add_index "activities_clients", ["activity_id"], name: "index_activities_clients_on_activity_id" 
    add_index "activities_clients", ["client_id"], name: "index_activities_clients_on_client_id" 

    create_table "clients", force: true do |t| 
    t.string "name" 
    t.string "avatar" 
    t.date  "birthdate" 
    t.string "background" 
    t.integer "group_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

어소시에이션이 실패한 이유는 무엇입니까?

답변

2

이는 대개 테스트 데이터베이스에서 실행되지 않은 데이터베이스 스키마 변경이있는 경우에 발생합니다. 최근 마이그레이션 후에 "rake db : test : prepare"를 실행해야합니다.

+0

그건 내 생각이었습니다. 복제도 작동하지 않았습니다. 내 schema.rb 내 게시물 editted했습니다. – CaptainCarl

+0

당신 has_many 점점 has_and_belongs_to_many 혼란스러워. 데이터베이스가 후자에 대해 설정되었지만 활동을 하나의 클라이언트에만 속할 수있는 has_many 관계로 사용하려고합니다. 무엇 이니? –

+0

흠. 아니; 이 코멘트를 편집 : 내 DB에 has_many이어야합니다. 어떻게 수정해야합니까? > _ < – CaptainCarl

관련 문제