2016-09-19 3 views
0

사용자 작업을 모방하고 서로를 의존하는 일련의 테스트가 있습니다.특정 rspec 테스트 그룹의 한가운데서 database_cleaner가 실행되지 않도록하려면 어떻게해야합니까?

database_cleaner을 정렬 테스트 중에 데이터베이스를 정리하지 않도록 구성하려면 어떻게합니까? 테스트

/spec/support/database_cleaner.rb: 

RSpec.configure do |config| 
    config.before(:suite) do 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
    DatabaseCleaner.strategy = :transaction 
    end 

    config.before(:each, js: true) do 
    DatabaseCleaner.strategy = :truncation 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 

샘플 :

RSpec.describe Conversation, type: :model, order: :defined do 
    context 'by default' do 
    before :context do 
     @alice = create :user 
     @bob = create :user 
     @subject = 'subject' 
     @body = 'body' 
     @conversation = @alice.send_message(@bob, @subject, @body) 
    end 

    it 'should have the subject it was opened with' do 
     expect(@conversation.subject).to eq @subject 
    end 

    it 'should have one message upon opening' do 
     expect(@conversation.messages.count).to eq 1 
    end 

    it 'should be initiated by @alice' do 
     expect(@conversation.initiator).to eq @alice 
    end 

    it 'should be received by @bob' do 
     expect(@conversation.recipient).to eq @bob 
    end 

    it 'should have two messages when bob sends a new message' do 
     @conversation.add_message(@bob, 'This is my second message') 
     expect(@conversation.messages.count).to eq 2 
    end 
end 

답변

0

를 수정했습니다. 내 테스트는 무작위로 실행됩니다. 단,이 테스트를 제외하면 order: :defined 태그가 있습니다. 그래서 나는 조건을 config.before 문에 support/database_cleaner.rb에 추가했습니다.

은 이제 다음과 같습니다

config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
관련 문제