Skip to content

Mocking something in the before block for minitest doesn't clear in other examples #86

Open
jperry opened this Issue Aug 7, 2012 · 1 comment

2 participants

@jperry
jperry commented Aug 7, 2012

Hi,

My test file looks like this with some other tests stripped out for readability:

describe 'Cookbooks' do

  describe 'TestRunner' do

    include RR::Adapters::RRMethods

    before(:each) do
       @run_list = 'role[base],role[testrun]'
       @box = "ubuntu"
       @test_runner = TestRunner.new(@run_list, @box)
    end

    describe '#initialize' do

      before(:each) do
        mock(Vagrant::Environment).new(:ui_class => Vagrant::UI::Colored, :cwd => @root_dir)
      end

     it "should do something" do
       some test here to use the mock
    end

    end

    describe '#run' do

      it "should run vagrant" do
        @test_runner.run
      end

    end

  end
end

Here is the code being tested:

module Cookbooks
    class TestRunner

      class TestRunnerException < Exception
      end

      attr_reader :run_list, :box

      # Initialize
      #
      # @param[String] run_list to use for test run
      # @param[String] box to use for test run (i.e. ubuntu, centos)
      def initialize(run_list, box)
        @run_list = run_list
        @box = box
        ENV['CHEF_RUN_LIST'] = @run_list
        @vagrant = Vagrant::Environment.new(:ui_class => Vagrant::UI::Colored, :cwd => ROOT_DIR)
      end

      # Execute test run by provisioning vagrant box
      # with run list provided
      def run
        begin
          @vagrant.cli("provision", @box)
        rescue Exception => e
          raise TestRunnerException, "Provisioning Failed: #{e}"
        end
      end
    end
end

The failure I get is this:

RR::Errors::TimesCalledError: new({:ui_class=>Vagrant::UI::Colored, :cwd=>"/Users/jperry/workspace/chef/chef-repo"})
Called 2 times.
Expected 1 times.
    /Users/jperry/.rvm/gems/ruby-1.9.2-p180@chef_repo/gems/rr-1.0.4/lib/rr/expectations/times_called_expectation.rb:49:in `verify_input_error'
    /Users/jperry/.rvm/gems/ruby-1.9.2-p180@chef_repo/gems/rr-1.0.4/lib/rr/expectations/times_called_expectation.rb:18:in `attempt'

The test in the '#run' example group fails because the mock thinks it is called a second time. Since the mock is in another example group wouldn't this have been cleared? Is this a limitation in minitest and not rr's fault?

Thanks,
Jay

@mcmire
Collaborator
mcmire commented Mar 15, 2013

There is a known issue with stubbing/mocking classes where they do not reset properly, see #43. This may be related to that. I'll keep this bug open for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.