2012-07-27 8 views
23

MySQL 데이터베이스를 사용하는 Rails 애플리케이션에서 테스트를 작성하는 데 RSpec을 처음 사용했습니다. 다음과 같이 내 비품을 정의 내 사양에서 그들을로드 오전 :RSpec의 비품

before(:all) do 
    fixtures :student 
end 

이 선언은 학생들 테이블에 내 비품에 정의 된 데이터를 저장 않거나 테스트하는 동안 그냥 테이블에 데이터를로드 않습니다 실행하고 모든 테스트가 실행 된 후 테이블에서 제거 하시겠습니까?

+10

조명기 대신 [factory_girl] (http://www.fabricationgem.org/) 또는 [fabrication] (http://www.fabricationgem.org/)에 시도해보십시오. –

답변

-1

RSpec을 구성하는 방법에 따라 다릅니다. 자세한 내용은 here을 참조하십시오.

1

before(:all)은로드/생성시 정확한 데이터를 유지합니다. 당신은 당신의 일을하고, 시험이 끝날 때까지 그것을 지냅니다. 그래서 bui 님의 링크가 after(:all)인데 그 결과를 삭제하거나 before(:each); @var.reload!;end을 사용하여 이전에 테스트 한 최신 데이터를 가져 오는 것입니다. 중첩 된 rspec 설명 블록에서이 접근법을 사용하는 것을 볼 수 있습니다.

16

당신은 블록 전에하지 않는 내에서, RSpec에와 비품을 사용 설명 블록에 비품을 지정하려면 :

describe StudentsController do 
    fixtures :students 

    before do 
    # more test setup 
    end 
end 
귀하의 학생기구는 학생 테이블에로드 한 다음에 롤백 얻을 것이다

각 테스트의 끝은 데이터베이스 트랜잭션을 사용합니다.

+1

https://www.relishapp.com/rspec/rspec-rails/docs/model-specs/transactional-examples#run-in-transactions-with-fixture – nruth

3

우선 : fixtures:all/:context/:suite hook에 사용할 수 없습니다. 이 후크 (post(:my_post)과 같은)에 조명기를 사용하지 마십시오.

설명 쓰기 (infuse write)로 설명/컨텍스트 블록에서만 조명기를 준비 할 수 있습니다.

전화

fixtures :students, :teachers 

는 DB에 데이터를로드하지 않습니다! 도우미 메서드 studentsteachers 만 준비하면됩니다. 요구 된 레코드는 처음 액세스하려고 시도 할 때 느리게로드됩니다. 오른쪽 직전

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이 다시 삽입됩니다.

+1

예! 모든 테스트를하기 전에 모든 조명기를로드하는 트릭을 발견했습니다. 그냥 RSpec.configure {| config | config.global_fixtures = : all} spec_helper에서 직접 테스트하면 모든 조명기에 액세스하려고 시도합니다. 이렇게하면 모든 조명기가 미리로드됩니다. – Foton