#56 open
James Mead

Stubbed method not being returned to its original state at the end of a test

Reported by James Mead | 2009-10-26 15:54:38 UTC

Reported on behalf of Gareth Reeves :-

Running the following code gives a single expectation error in TestB#setup :-

  unexpected invocation: B.new()

It's like the stub expectation on B in TestA doesn't get removed when other tests run. While A (the superclass) gets cleaned up.

require 'rubygems'
require 'test/unit'
require 'mocha'

class A
  def value; 'a'; end
end

class B < A; end


class TestA < Test::Unit::TestCase
  def setup
    A.stubs( :new ).returns( 'this is a' )
    B.stubs( :new ).returns( 'this is b' )
    @a = A.new
    @b = B.new
  end

  def test_value
    assert_equal 'this is a', @a
    assert_equal 'this is b', @b
  end
end

class TestB < Test::Unit::TestCase
  def setup
    @b = B.new
  end

  def test_value
    assert_equal 'a', @b.value
  end
end

class TestAgain < Test::Unit::TestCase
  def setup
    @a = A.new
  end

  def test_value
    assert_equal 'a', @a.value
  end
end

Comments and changes to this ticket

  • James Mead

    James Mead 2009-10-26 15:56:17 UTC

    • State changed from “new” to “open”

    I wonder whether it's significant that it's the new method that is being stubbed.

  • James Mead

    James Mead 2009-10-26 16:00:53 UTC

    It doesn't appear to be significant that it's the new method that is being stubbed. The following test fails with a similar error :-

      unexpected invocation: B.wibble()
    
    require 'rubygems'
    require 'test/unit'
    require 'mocha'
    
    class A
      def self.wibble; new; end
      def value; 'a'; end
    end
    
    class B < A; end
    
    
    class TestA < Test::Unit::TestCase
      def setup
        A.stubs( :wibble ).returns( 'this is a' )
        B.stubs( :wibble ).returns( 'this is b' )
        @a = A.wibble
        @b = B.wibble
      end
    
      def test_value
        assert_equal 'this is a', @a
        assert_equal 'this is b', @b
      end
    end
    
    class TestB < Test::Unit::TestCase
      def setup
        @b = B.wibble
      end
    
      def test_value
        assert_equal 'a', @b.value
      end
    end
    
    class TestAgain < Test::Unit::TestCase
      def setup
        @a = A.wibble
      end
    
      def test_value
        assert_equal 'a', @a.value
      end
    end
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

A mocking & stubbing library for Ruby.

* GitHub repository
* Documentation
* Mailing List

People watching this ticket

Tags

Pages