Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

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

#56

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

Reported by James Mead · October 26th, 2009 @ 03:54 PM

State: open
Milestone: none
Assigned to: James Mead James Mead

Activity

  1. James Mead
    James Mead
    • State changed from new to open

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

    October 26th, 2009 @ 03:56 PM

  2. James Mead
    James Mead

    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
    

    October 26th, 2009 @ 04:00 PM

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.