codebee
codebee

Reputation: 844

Multiple 'before' blocks in Minitest 'describe' block

I am trying to have two 'before' blocks under a describe block, but only the latter one runs.

describe '#bunch_of_tests' do

  before(:all) do
    p 'do this before all tests'
  end

  before(:each) do
    p 'do this before each test'
  end

describe 'this is 1st test' do
  it 'runs 1st test' do
  end
end

describe 'this is 2nd test' do
  it 'runs 2nd test' do
  end
end

end

The problem is that this statement never gets printed: 'do this before all tests'

My expectation is that this should run once before all the tests. 'before(:each)' block is working as expected.

Upvotes: 0

Views: 1048

Answers (1)

jvillian
jvillian

Reputation: 20253

This may be clumsy, but you could try:

RSpec.describe '#bunch_of_tests' do

  before(:all) do
    p 'do this before all tests'
  end

  describe "before each" do 
    before(:each) do
      p 'do this before each test'
    end

    describe 'this is 1st test' do
      it 'runs 1st test' do
      end
    end

    describe 'this is 2nd test' do
      it 'runs 2nd test' do
      end
    end
  end

end

Which yields:

#bunch_of_tests
"do this before all tests"
  before each
    this is 1st test
"do this before each test"
      runs 1st test
    this is 2nd test
"do this before each test"
      runs 2nd test

Finished in 0.40184 seconds (files took 3.5 seconds to load)
2 examples, 0 failures

Upvotes: 1

Related Questions