New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't stub singleton methods of modules with 4.7.2 #272

Closed
ghost opened this Issue Apr 19, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Apr 19, 2013

With MiniTest 4.7.2:

gem 'minitest', '4.7.2'
require 'minitest/mock'

Math.stub(:log10, 'STUBBED!') { Math.log10 1000 } # => 3.0

Whereas with MiniTest 4.7.1:

gem 'minitest', '4.7.1'
require 'minitest/mock'

Math.stub(:log10, 'STUBBED!') { Math.log10 1000 } # => "STUBBED!"

Is this considered an acceptable side effect of 8bc0037?

@zenspider

This comment has been minimized.

Show comment
Hide comment
@zenspider

zenspider Apr 19, 2013

Member

On Apr 18, 2013, at 18:44 , Aaron Beckerman notifications@github.com wrote:

With MiniTest 4.7.2:

gem 'minitest', '4.7.2'
require 'minitest/mock'

Math.stub(:log10, 42.0) { Math.log10 1000 } # => 3.0
Whereas with MiniTest 4.7.1:

gem 'minitest', '4.7.1'
require 'minitest/mock'

Math.stub(:log10, 42.0) { Math.log10 1000 } # => 42.0
Is this considered an acceptable side effect of 8bc0037?

Grrr....

No. That was supposed to fix the stubbing of things like Kernel.sleep... I'm never gonna win. :(

I'm knee deep in a minitest revamp and can't get my head out of this for a bit... Can you take a whack at it? I dunno if there is ever gonna be a single way to cover all cases and that's ok.

Member

zenspider commented Apr 19, 2013

On Apr 18, 2013, at 18:44 , Aaron Beckerman notifications@github.com wrote:

With MiniTest 4.7.2:

gem 'minitest', '4.7.2'
require 'minitest/mock'

Math.stub(:log10, 42.0) { Math.log10 1000 } # => 3.0
Whereas with MiniTest 4.7.1:

gem 'minitest', '4.7.1'
require 'minitest/mock'

Math.stub(:log10, 42.0) { Math.log10 1000 } # => 42.0
Is this considered an acceptable side effect of 8bc0037?

Grrr....

No. That was supposed to fix the stubbing of things like Kernel.sleep... I'm never gonna win. :(

I'm knee deep in a minitest revamp and can't get my head out of this for a bit... Can you take a whack at it? I dunno if there is ever gonna be a single way to cover all cases and that's ok.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Apr 19, 2013

As I understand it, the user wants to test code like this:

class SlowClapper
  def slow_clap
    sleep 3
    :clap
  end
end

But he doesn't want the sleep to happen during the tests. Is that the motivation?

Here's how I would do it:

class SlowClapperTest < MiniTest::Unit::TestCase
  def test_slow_clap
    slow_clapper = SlowClapper.new
    slow_clapper.stub :sleep, nil do |fast_clapper|
      assert_equal :clap, fast_clapper.slow_clap
    end
  end
end

In other words, I would stub (the singleton class of) the object that actually receives the "sleep" message -- slow_clapper itself -- not a module assumed to be somewhere down its ancestor list.

So, as far as this example is concerned, I don't see a need for 8bc0037. But am I misunderstanding the use case, or is there another use case you're trying to support?

ghost commented Apr 19, 2013

As I understand it, the user wants to test code like this:

class SlowClapper
  def slow_clap
    sleep 3
    :clap
  end
end

But he doesn't want the sleep to happen during the tests. Is that the motivation?

Here's how I would do it:

class SlowClapperTest < MiniTest::Unit::TestCase
  def test_slow_clap
    slow_clapper = SlowClapper.new
    slow_clapper.stub :sleep, nil do |fast_clapper|
      assert_equal :clap, fast_clapper.slow_clap
    end
  end
end

In other words, I would stub (the singleton class of) the object that actually receives the "sleep" message -- slow_clapper itself -- not a module assumed to be somewhere down its ancestor list.

So, as far as this example is concerned, I don't see a need for 8bc0037. But am I misunderstanding the use case, or is there another use case you're trying to support?

@tyabe

This comment has been minimized.

Show comment
Hide comment
@tyabe

tyabe Apr 20, 2013

The following example broken:

gem 'minitest' # '4.7.2'
require 'minitest/mock'
require 'minitest/autorun'

module Foo
  def self.say
    "Hello"
  end
end

class TestSample < MiniTest::Unit::TestCase
  def test_say
    Foo.stub(:say, 'STUBBED!') do
      assert_equal 'STUBBED!', Foo.say
    end
  end
end

errors:

NameError: undefined method `say' for module `Foo'

tyabe commented Apr 20, 2013

The following example broken:

gem 'minitest' # '4.7.2'
require 'minitest/mock'
require 'minitest/autorun'

module Foo
  def self.say
    "Hello"
  end
end

class TestSample < MiniTest::Unit::TestCase
  def test_say
    Foo.stub(:say, 'STUBBED!') do
      assert_equal 'STUBBED!', Foo.say
    end
  end
end

errors:

NameError: undefined method `say' for module `Foo'
@zenspider

This comment has been minimized.

Show comment
Hide comment
@zenspider

zenspider Apr 21, 2013

Member

Fixed. Thanks guys. Sorry.

Member

zenspider commented Apr 21, 2013

Fixed. Thanks guys. Sorry.

@zenspider zenspider closed this Apr 21, 2013

@seattlerb seattlerb locked and limited conversation to collaborators May 17, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.