Database Cleaner not working in minitest rails
Asked Answered
W

5

8

My Minitest controller tests are working fine if I run them alone using rake minitest:controllers but when I run rake minitest:all then I get validation failed error. It is because email is already used in model tests. I used DatabaseCleaner to clean the database but unable to clean database.

My code for database cleaner:

require "database_cleaner"
DatabaseCleaner.strategy = :transaction

class MiniTest::Rails::ActionController::TestCase
    include Devise::TestHelpers

    def setup
      DatabaseCleaner.start
    end

    def teardown
      DatabaseCleaner.clean
    end
Watchband answered 28/3, 2013 at 5:52 Comment(0)
R
15

Short answer:

gem install "minitest-around"

Long answer:

before/after or setup/teardown in minitest are NOT hooks as in rspec, therefore you can't have multiple before/after or setup/teardown in minitest, since what they do is just redefining the method.

To solve this issue, you can use minitest-around, which adds support for multiple before/after or setup/teardown and around, simply add the gem to your test group:

# put in your Gemfile
gem 'minitest-around', group: :test

For setting up the database_cleaner, you can have it as you want, following is an example of the setup:

# tests/support/database_cleaner.rb
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)

class Minitest::Rails::ActionController::TestCase
  def setup
    DatabaseCleaner.start
  end

  def teardown
    DatabaseCleaner.clean
  end
end

And in your test files:

# tests/your/test/file_test.rb
require 'support/database_cleaner'

# assertions here ...

That's it, see the Github for detailed info.

Reckon answered 16/2, 2015 at 10:36 Comment(1)
Note that you CAN have multiple 'setups' through calling super in the child test's setup method.Tabber
I
5

If for whatever reason you don't want to add the 'minitest-around' gem (to have more than one setup and teardown method), you can do this in your test_helper.rb...

require "database_cleaner"
DatabaseCleaner.strategy = :transaction

module AroundEachTest
  def before_setup
    super
    DatabaseCleaner.start
  end

  def after_teardown
    super
    DatabaseCleaner.clean
  end
end

class Minitest::Test
  include AroundEachTest
end
Ideology answered 2/3, 2018 at 19:56 Comment(1)
This was simple enough without having to add an extra gemLatrinalatrine
W
4

I found my mistake ,may be it helps someone else ..

I should write DatabaseCleaner.start in setup of every model test where setup is defined, as i am overwriting setup method in every test file.

Watchband answered 28/3, 2013 at 8:56 Comment(3)
You can add minitest-around to add multiple before/after.Reckon
@HendraUzia can you add example code and answer it.Watchband
Don't do that, use a subclass & call super before any setup you need - see my answer for an illustration.Solus
S
2

This is why I like Minitest; no fancy DSL to block thinking about how to use Ruby properly.

My setup is as follows:

In test_helper.rb

class MyTest < Minitest::Test
  def setup
    DatabaseCleaner.start
  end

  def teardown
    DatabaseCleaner.clean
  end
end

Then I just subclass this in any test that need database cleaning. Note the call super first cleans the db before any subclass-specific setup. The same call to super would need to be included in any subclass teardown method, but this can usually be omitted entirely.

class FooTest < MyTest
  def setup
    super
    @foo = Foo.new(bar: 'whatever')
  end

  def test_save
    @foo.save
    assert_equal 1, Foo.count
  end
end

If I need to subclass MyTest further (e.g. for integration tests) I include its own setup & teardown methods with calls to super so it goes right up the inheritance tree.

Solus answered 4/8, 2018 at 0:56 Comment(0)
F
0

You can DRY up that repetition with this

DatabaseCleaner.strategy = :truncation
class MiniTest::Spec
  before :each do
    DatabaseCleaner.clean
  end
end

This example subclasses the spec runner, but you can pick your test environment of choice.

Fastening answered 17/7, 2014 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.