2016-09-19 1 views
0

Ich habe eine Reihe von geordneten Tests, die Benutzeraktionen imitieren und voneinander abhängen.Wie sage ich database_cleaner nicht in der Mitte einer bestimmten Gruppe von rspec Tests

Wie konfiguriere ich database_cleaner, um die Datenbank mitten in diesen geordneten Tests nicht zu säubern?

/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 

Probe von Tests:

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 

Antwort

0

es behoben. Meine Tests laufen zufällig, außer diesen haben sie das order: :defined Tag. Also fügte ich eine unless Bedingung zu der config.before Anweisung in support/database_cleaner.rb hinzu.

Es sieht nun wie folgt aus:

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

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