Reputation: 13467
I have 3 questions:
What is the default testing library that comes with Rails? I can't actually see what it's called... MiniTest? TestUnit?
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.
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
Reputation: 9246
I think you should separate this into 3 posts as it's easier.
ActiveSupport::TestCase
extends Minitest::Test
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
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