2014-06-09 6 views
3

나는 단위 테스트를 처음 접했고 간단한 유스 케이스가있다.레일 테스팅 - has_many 연관성을위한 비품

도시, 거주자입니다. 한 도시에는 많은 주민이 있습니다.

2 개의 fixture yml 파일을 생성했습니다 : cities.yml, residents.yml.

Minitest::UnexpectedError: ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'residents' in 'field list': INSERT INTO `cities` (`name`, `residents`, `created_at`, `updated_at`, `id`) VALUES ('city1', 'resident1, resident2, resident3', '2014-06-09 20:42:22', '2014-06-09 20:42:22', 574963714) 
: 난 항상 통과해야 사소한 테스트를 실행하면

residents.yml

resident1: 
    name: resident1 

resident2: 
    name: resident2 

resident3: 
    name: resident3 

cities.yml는, 나는 오류를 얻을 수

city1: 
    name: city1 
    residents: resident1, resident2, resident3 

무엇이 일까요?은 속성이 name: 'city1' 인 City 모델 인스턴스와 속성이 residents이고 거주 모델 인스턴스가 3 개 배열되어야합니다.

City.rb에서 거주 모델과 has_many 관계를 지정했습니다. 그리고 내가 지정한 Resident.rb 내부는 City 모델과 관련되어 있습니다.

이 작업은 간단해야합니다. 그렇습니까?

업데이트 # 1 :

주민 설비에 대한 도시의 속성을 설정하여 그것을 할 만 가능 보인다.

residents.yml

resident1: 
    name: resident1 
    city: city1 # added 

resident2: 
    name: resident2 
    city: city1 # added 

resident3: 
    name: resident3 
    city: city1 # added 

cities.yml

city1: 
    name: city1 
    # residents: resident1, resident2, resident3 

나는 그것이 충분해야 할 것 같아요.

+1

레일과 함께 말과 캐리지의 고정물이 나옵니다. Factory Girl 젬과이 튜토리얼을 http://everydayrails.com/2012/03/12/testing-series-intro.html으로 시작하는 것이 좋습니다. 비품은 많은 상흔을 일으킬 수 있습니다. – steel

답변

1

업데이트 # 1이 괜찮다고 생각합니다. 결국 연관성이 실제로 DB에 저장되는 방식이며 조명기는 단순히 DB를 시드하는 방법 일뿐입니다.

정말로 원한다면 FactoryGirl 또는 코드를 사용하여 건조시킬 수 있지만 테스트는 DAMP not DRY으로되어 있으며 조명기까지 확장됩니다.