Tallboy
Tallboy

Reputation: 13467

ActiveSupport::TestCase won't run DB commands?

I have 3 questions:

  1. What is the default testing library that comes with Rails? I can't actually see what it's called... MiniTest? TestUnit?

  2. How can I make some code run once per TestCase to setup for ALL tests. It seems setup do is run before each test, but I want to set up for the entire file.

  3. How come my example below doesn't work (it won't clear out and re-create the seed data before each test)

Code:

require 'test_helper'

# negative numbers
# 3+ line item journal entries

class LedgerTest < ActiveSupport::TestCase
  setup do
    ActiveRecord::Base.subclasses.each(&:delete_all)

    AccountType.create!(code: 101, name: 'Cash',                     category: :asset,     normal_balance: :debit)
    AccountType.create!(code: 102, name: 'Equipment',                category: :asset,     normal_balance: :debit)
    AccountType.create!(code: 103, name: 'Accumulated Depreciation', category: :asset,     normal_balance: :credit, contra: true)
  end

  test "test 1 here" do
  end

  test "test 2 here" do
  end
end

Upvotes: 0

Views: 205

Answers (1)

Igor Pantović
Igor Pantović

Reputation: 9246

I think you should separate this into 3 posts as it's easier.

1 ActiveSupport::TestCase framework

ActiveSupport::TestCase extends Minitest::Test

2 Setup before all examples in a file

As far as I know, all the ways to do it are workarounds. This is discouraged and not supported through a nice API by default. Quick search comes up with this:

setup_executed = false
setup do
  unless setup_executed
    #code goes here
    setup_executed = true
  end
end

3 Setup not working

In development and test environments by default classes are autoloaded lazily, as they are required. Unless you reference a class somewhere in your code before running ActiveRecord::Base.subclasses, ruby won't know it exists, because it was never loaded.

Upvotes: 2

Related Questions