우선 : fixtures
은 :all
/:context
/:suite hook
에 사용할 수 없습니다. 이 후크 (post(:my_post)
과 같은)에 조명기를 사용하지 마십시오.
설명 쓰기 (infuse write)로 설명/컨텍스트 블록에서만 조명기를 준비 할 수 있습니다.
전화
fixtures :students, :teachers
는 DB에 데이터를로드하지 않습니다! 도우미 메서드 students
및 teachers
만 준비하면됩니다. 요구 된 레코드는 처음 액세스하려고 시도 할 때 느리게로드됩니다. 오른쪽 직전
dan=students(:dan)
이렇게하면 학생과 교사가 delete all from table + insert fixtures
방법으로로드됩니다.
그래서 일부 학생을 이전 (: 문맥) 훅으로 준비하면 지금 사라질 것입니다 !!
레코드 삽입은 테스트 스위트에서 한 번만 수행됩니다.
테스트 스위트의 끝에서 조명기의 레코드는 삭제되지 않습니다. 다음 테스트 스위트 실행시 삭제되고 다시 삽입됩니다.
예 : 다음 시험 (다음 스위트 룸)이 순서대로 다시 실행하는 경우 테스트의 모든 기대가 위의 기대보다
을 전달합니다
#students.yml
dan:
name: Dan
paul:
name: Paul
#teachers.yml
snape:
name: Severus
describe Student do
fixtures :students, :teachers
before(:context) do
@james=Student.create!(name: "James")
end
it "have name" do
expect(Student.find(@james.id).to be_present
expect(Student.count).to eq 1
expect(Teacher.count).to eq 0
students(:dan)
expect(Student.find_by_name(@james.name).to be_blank
expect(Student.count).to eq 2
expect(Teacher.count).to eq 1
end
end
#but when fixtures are in DB (after first call), all works as expected (by me)
describe Teacher do
fixtures :teachers #was loade in previous tests
before(:context) do
@james=Student.create!(name: "James")
@thomas=Teacher.create!(name: "Thomas")
end
it "have name" do
expect(Teacher.find(@thomas.id).to be_present
expect(Student.count).to eq 3 # :dan, :paul, @james
expect(Teacher.count).to eq 2 # :snape, @thomas
students(:dan)
expect(Teacher.find_by_name(@thomas.name).to be_present
expect(Student.count).to eq 3
expect(Teacher.count).to eq 2
end
end
expect(Student.count).to eq 1
이 충족되지 않습니다! 3 명의 학생들이있을 것입니다 (: dan, : paul 그리고 신선한 새로운 @james). 모든 파일은 students(:dan)
전에 삭제되며 : paul 및 : dan이 다시 삽입됩니다.
조명기 대신 [factory_girl] (http://www.fabricationgem.org/) 또는 [fabrication] (http://www.fabricationgem.org/)에 시도해보십시오. –