bergyman
bergyman

Reputation: 4557

Is there an equivalent to RSpec's before(:all) in MiniTest?

Since it now seems to have replaced TestUnit in 1.9.1, I can't seem to find an equivalent to this. There are times when you really just want a method to run once for the suite of tests. For now I've resorted to some lovely hackery along the lines of:

Class ParseStandardWindTest < MiniTest::Unit::TestCase
  @@reader ||= PolicyDataReader.new(Time.now)  
  @@data ||= @@reader.parse  
  def test_stuff  
    transaction = @@data[:transaction]  
    assert true, transaction  
  end  
end

Upvotes: 3

Views: 1238

Answers (2)

Dave Sag
Dave Sag

Reputation: 13486

It's best to use 'let' I've found.

eg (using minitest/spec)

describe "my amazing test" do

  let(:reader) { PolicyDataReader.new(Time.now) }
  let(:data) {reader.parse}

  it "should parse" do
    transaction = data[:transaction]
    transaction.must_equal true
  end

end

to use minitest/spec simply add

gem 'minitest', require: ['minitest/autorun', 'minitest/spec']

to the test group of your Gemfile

Upvotes: 0

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40313

Nops, there's only setup and teardown and both are run before/after every test. But your solution seems to do the trick.

Upvotes: 3

Related Questions