[mocha-developer] Stubbing constructiors

Tomas Pospisek tpo2 at sourcepole.ch
Thu Dec 14 08:27:01 EST 2006


This works:

    class X
      def X.initialize( stuff )
      end
    end

    X.initialize("bla")


However stubbing it doesn,t:

    require 'test/unit'
    require 'stubba'

    class X
      def X.initialize( stuff )
      end
    end

   class XTest < Test::Unit::TestCase
     def test_
       X.stubs(:initialize).with("bla")
       X.initialize("bla")
     end
   end

Ruby lets me know that:

   Loaded suite /tmp/foo
   Started
   E(eval):1: warning: removing `initialize' may cause serious problem

   Finished in 0.002625 seconds.

     1) Error:
   test_(XTest):
   NoMethodError: private method `initialize' called for X:Class
      /tmp/kak.rb:12:in `test_'

   1 tests, 0 assertions, 0 failures, 1 errors

Routing around the problem is possible:

  require 'test/unit'
  require 'stubba'

  class X
    def X.initialize( stuff )
    end
  end

  class XTest < Test::Unit::TestCase
    def test_
      X.stubs(:initialize).with("bla")
      X.public_class_method :initialize
      X.initialize("bla")
    end
  end

...however a bit awkward...

:-)
*t



----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


More information about the mocha-developer mailing list