[mocha-developer] problem with using any_instance

Yi Wen hayafirst at gmail.com
Fri Mar 30 14:24:09 EDT 2007


I use it as a rails plugin and I am running on 0.30

I put this a_test.rb under RAILS_PROJ_ROOT/test/unit

If running test_a and test_b separately, they work fine, but if running the
whole test suite, it gave me this error:

output "called" is a string I puts in unstub of AnyInstanceMethod

Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.2
/lib/rake/rake_test_loader
Started
"called"
.E
Finished in 0.011905 seconds.


  1) Error:
test_b(ATest):
NoMethodError: undefined method `method_name' for #<A:0xb6fa0ad0>
    /home/ywen/projects/my_proj/test/unit/a_test.rb:6:in `send'
    /home/ywen/projects/my_proj/test/unit/a_test.rb:6:in `initialize'
    /home/ywen/projects/my_proj/test/unit/a_test.rb:21:in `new'
    /home/ywen/projects/my_proj/test/unit/a_test.rb:21:in `test_b'

/home/ywen/projects/my_proj/config/../vendor/plugins/mocha/lib/mocha/test_case_adapter.rb:19:in
`__send_
_'

/home/ywen/projects/my_proj/config/../vendor/plugins/mocha/lib/mocha/test_case_adapter.rb:19:in
`run'

Thanks!

Yi


--------------------------------------------------------------------------------
a_test.rb


require File.dirname(__FILE__) + '/../test_helper'

class A
  attr_reader :b
    def initialize
        @b = send('method_name')
    end
    private
    def method_name
        "Original"
    end
end

class ATest < Test::Unit::TestCase
  def test_a
    A.any_instance.stubs(:method_name).returns("stubs")
    assert_equal "stubs", A.new.b
  end

  def test_b
    assert_equal "Original", A.new.b
  end
end

On 3/30/07, James Mead <jamesmead44 at gmail.com> wrote:
>
> I've just tried to recreate your problem...
>
> require 'test/unit'
> require 'rubygems'
> require 'mocha'
>
> class AnyInstanceTest < Test::Unit::TestCase
>
>   class Klass
>     def method_name
>       "original"
>     end
>   end
>
>   def test_a
>     Klass.any_instance.stubs(:method_name).returns("something")
>     assert_equal "something", Klass.new.method_name
>   end
>
>   def test_b
>     assert_equal "original", Klass.new.method_name
>   end
>
> end
>
> ...but both tests pass. So I don't understand why you are having a
> problem.
>
> Please send us all the code for the simplest self-contained test case
> which
> illustrates your problem?
>
> What version of Mocha are you using? And are you using it as a gem or as
> Rails plugin?
>
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list