dax
dax

Reputation: 11017

how to call a method immediately before the start of a test run in minitest

I have the following scenario in testing a large, distributed system via end-to-end tests:

Class A < Minitest::Spec

  before do 
    # stuff

    setup_runtime_environment
  end

end

Class B < Class A

  before do 
    # stuff
  end

end

Class C < Class B

  before do 
    # stuff
  end

end

Some tests only inherit from Class A - it is the most general and includes things which are relevant to testing the entire system.

Class B is more specific - it includes things which are relevant to a large group of tests, but still needs to be setup by the setup_runtime_environment method in Class A

Class C is the most specific - it includes things which are relevant only to a small group of tests, but still needs to be setup by the setup_runtime_environment method in Class A

The problem is that the runtime environment, for various test purposes, needs to be configured differently when running 'overall' tests vs. 'general' tests vs. 'specific' tests, and it needs to be configured before it is initialized.

So I could add the setup_runtime_environment method to the end of each before block. But more ideal would be a way to run that method immediately before the specs are run. A 'just_before' block. Does anyone know of a way to do this?

To clarify:

If I'm running an 'overall' spec - one that inherits only from Class A, the stack looks something like this, assuming 'A' is the before block in Class A and 's' is setup_runtime_environment:

A -> s -> running of specs

Likewise if I'm running a specific spec - one that inherits from Class C, the stack would look something like this:

A -> s -> B -> C -> running of specs

I want 's' to be called at the very end of any chain:

A -> s -> running of specs  

or

A -> B -> s -> running of specs  

or

A -> B -> C -> s -> running of specs  

which is what I mean by 'just before' - as in just before the specs are run.

Upvotes: 0

Views: 331

Answers (1)

blowmage
blowmage

Reputation: 8984

You are limited because of the behavior of the spec DSL. If you want to really control when your setup code is run along with the inherited setup, you should use the test-style setup and super.

Class A < Minitest::Spec

  def setup
    # do stuff
    setup_runtime_environment
  end

end

Class B < Class A

  def setup
    super
    # do stuff after setup_runtime_environment
  end

end

Class C < Class B

  def setup
    # do stuff before setup_runtime_environment
    super
  end

end

Upvotes: 1

Related Questions