Skip to content

Instantly share code, notes, and snippets.

@iwan
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwan/a8c8c3c2a197a785dc09 to your computer and use it in GitHub Desktop.
Save iwan/a8c8c3c2a197a785dc09 to your computer and use it in GitHub Desktop.
Spec results with and without database cleaning

Spec

require 'rails_helper'

describe "Foo", type: :model do
  it "ex3" do
    puts "--ex3--"
    puts "rep: #{@rep.inspect}"
    puts "Report count: #{Report.count}"
  end

  context "c1" do
    before :context do
      puts "creating a report"
      @rep = create :report
    end

    it "ex1" do
      puts "--ex1--"
      puts "rep: #{@rep.inspect}"
      puts "Report count: #{Report.count}"
    end
  end

  it "ex2" do
    puts "--ex2--"
    puts "rep: #{@rep.inspect}"
    puts "Report count: #{Report.count}"
  end
end

Result without database_cleaner

Foo
--ex3--
rep: nil
Report count: 2
  ex3
--ex2--
rep: nil
Report count: 2
  ex2
  c1
creating a report
--ex1--
rep: #<Report id: 3, name: "Report 1", year: 2015, description: "et", created_at: "2015-07-24 10:20:30", updated_at: "2015-07-24 10:20:30">
Report count: 3
    ex1

Result with database_cleaner

Foo
Foo
--ex3--
rep: nil
Report count: 0
  ex3
--ex2--
rep: nil
Report count: 0
  ex2
  c1
creating a report
--ex1--
rep: #<Report id: 1, name: "Report 1", year: 2015, description: "dolor", created_at: "2015-07-24 10:28:23", updated_at: "2015-07-24 10:28:23">
Report count: 1
    ex1

DatabaseCleaner config:

config.before(:suite) do
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)
end

config.around(:each) do |example|
  DatabaseCleaner.cleaning do
    example.run
  end
end

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

config.after(:context) do
  DatabaseCleaner.clean
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment