Feature Requests: Browse | Submit New | Admin

[#23055] stubs(with_hash) not working with existing object

Date:
2008-12-01 20:49
Priority:
3
Submitted By:
Nobody
Assigned To:
Nobody (None)
Category:
None
State:
Closed
Summary:
stubs(with_hash) not working with existing object

Detailed description
with Mocha 0.9.3:

test file:
-----
require'test/unit'
require 'rubygems'
require 'mocha'

class A
  def meth
    'hi'
  end
end

class MochaTest < Test::Unit::TestCase

  def test_stubs_no_hash
    a = A.new
    a.stubs(:meth).returns('hello')
    a.stubs(:meth2).returns('bye')

    assert_equal 'hello', a.meth
    assert_equal 'bye', a.meth2
  end


  def test_stubs_with_hash_and_override
    a = A.new
    a.stubs(:meth => 'hello')

    assert_equal 'hello', a.meth
  end

  def test_stubs_with_hash_and_no_override
    a = A.new
    a.stubs(:meth2 => 'bye')

    assert_equal 'bye', a.meth2
  end
end
-------


result:

Riheligan:test tobias$ ruby unit/mocha_test.rb
Loaded suite unit/mocha_test
Started
.EF
Finished in 0.003917 seconds.

  1) Error:
test_stubs_with_hash_and_no_override(MochaTest):
NoMethodError: undefined method `meth2' for #<A:0x12d0bbc @mocha=nil>
    unit/mocha_test.rb:34:in `test_stubs_with_hash_and_no_override'
    /opt/local/lib/ruby/gems/1.8/gems/mocha-0.9.3/lib/mocha/test_case_adapter.rb:69:in `__send__'
    /opt/local/lib/ruby/gems/1.8/gems/mocha-0.9.3/lib/mocha/test_case_adapter.rb:69:in `run'

  2) Failure:
test_stubs_with_hash_and_override(MochaTest)
    [unit/mocha_test.rb:27:in `test_stubs_with_hash_and_override'
     /opt/local/lib/ruby/gems/1.8/gems/mocha-0.9.3/lib/mocha/test_case_adapter.rb:69:in `__send__'
     /opt/local/lib/ruby/gems/1.8/gems/mocha-0.9.3/lib/mocha/test_case_adapter.rb:69:in `run']:
<"hello"> expected but was
<"hi">.

3 tests, 3 assertions, 1 failures, 1 errors

Add A Comment: Notepad

Please login


Followup

Message
Date: 2008-12-05 13:42
Sender: James Mead

Thanks for checking it. I'll try and get a release out in the
not to distant future. Thanks also for the acceptance tests -
however I already added these two [1]. Because I know that all
3 of your acceptance tests invoke the same implementation in
Mocha::ObjectMethods, I decided that these two tests are sufficient.
I'll try and close the feature request on your behalf.

Many thanks, James.
http://blog.floehopper.org

[1] http://github.com/floehopper/mocha/commit/1b3ed16c7755f8a5480
87d2d31bbcb44e2315281#diff-3
Date: 2008-12-05 13:21
Sender: Tobias Crawley

Works for me, at least in the simple spots were I used it in
my tests. I also wrote an acceptance test for this if you want
it. Thanks for your hard work!

I'll try to close the request, but may not be able to, since
I entered it anonymously.

stubs_consistency_test.rb:
----
require File.join(File.dirname(__FILE__),
"acceptance_test_helper")
require 'mocha'

class StubsConsistencyTest < Test::Unit::TestCase

  include AcceptanceTest

  def setup
    setup_acceptance_test
  end

  def teardown
    teardown_acceptance_test
  end

  class SomeClass

    def my_method
      false
    end

  end

  def test_mocks_should_allow_hash_based_stubbing
    object = mock('SomeClass')
    object.stubs(:my_method => true, :another_method? =>
true)

    assert object.my_method
    assert object.another_method?
  end

  def test_object_mocks_should_allow_hash_based_stubbing
    object = SomeClass.new
    object.stubs(:my_method => true, :another_method? =>
true)

    assert object.my_method
    assert object.another_method?
  end

  def test_any_instance_should_allow_hash_based_stubbing
    SomeClass.any_instance.stubs(:my_method => true,
:another_method? => true)
    object = SomeClass.new

    assert object.my_method
    assert object.another_method?
  end
end
Date: 2008-12-02 17:42
Sender: Tobias Crawley

Thanks James! I'll grab a copy and play with it sometime in the
next couple of days.
Date: 2008-12-02 17:24
Sender: James Mead

Sorry I forgot to add the link [1] to my previous comment.

[1] http://github.com/floehopper/mocha/commit/1b3ed16c7755f8a5480
87d2d31bbcb44e2315281
Date: 2008-12-02 17:08
Sender: James Mead

I've just committed a solution [1]. It would be great if you
could try it out and see if it works for you. If you are happy
with the solution, please close this feature request.

Thanks, James.
http://blog.floehopper.org
Date: 2008-12-02 16:56
Sender: James Mead

I hope you don't mind, but in the light of your last comment,
I've converted this bug report into a feature request.

Cheers, James.
http://blog.floehopper.org
Date: 2008-12-01 20:57
Sender: Tobias Crawley

Nevermind - I see I was reading the docs for Mock#stubs, instead
of ObjectMethods#stubs. It would be handy and much less confusing
if ObjectMethods#stubs behaved like Mock#stubs when passed a
Hash, or at least raised and error.

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

Field Old Value Date By
status_idOpen2008-12-05 13:42jamesmead
close_date2008-12-05 13:422008-12-05 13:42jamesmead