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

Stubbing Kernel methods does not work correctly when they are called via "send" #134

Closed
camski opened this Issue Feb 9, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@camski

camski commented Feb 9, 2013

Some examples:

describe 'Kernel method: warn' do
  it 'fails when called via "send"' do
    object = stub
    object.expects(:warn).with('blah')
    object.send(:warn, 'blah')
  end

  it 'also fails with "stub_everything"' do
    object = stub_everything
    object.expects(:warn).with('blah')
    object.send(:warn, 'blah')
  end

  it 'succeeds when called directly' do
    object = stub
    object.expects(:warn).with('blah')
    object.warn('blah')
  end
end

describe 'non-Kernel method: warning' do
  it 'succeeds' do
    object = stub
    object.expects(:warning).with('blah')
    object.send(:warning, 'blah')
  end
end

describe 'sleep' do
  it 'also fails when called via "send"' do
    object = stub
    object.expects(:sleep).with(1)
    object.send(:sleep, 1)
  end
end

The failure cases all seem unexpected. It looks like the expectations are not correctly intercepting calls to Kernel methods that are dispatched via "send". Note that a non-Kernel method ("warning") does work as expected.

@camski

This comment has been minimized.

Show comment
Hide comment
@camski

camski Feb 9, 2013

There is a workaround: Define the method we want stubbed directly on the object, then add an expectation.

describe 'workaround' do
  it 'works' do
    object = stub
    def object.warn(msg)
      puts 'This is not Kernel.warn'
    end
    object.expects(:warn).with('blah')
    object.send(:warn, 'blah')
  end
end

camski commented Feb 9, 2013

There is a workaround: Define the method we want stubbed directly on the object, then add an expectation.

describe 'workaround' do
  it 'works' do
    object = stub
    def object.warn(msg)
      puts 'This is not Kernel.warn'
    end
    object.expects(:warn).with('blah')
    object.send(:warn, 'blah')
  end
end

@ghost ghost assigned floehopper Feb 10, 2013

@floehopper

This comment has been minimized.

Show comment
Hide comment
@floehopper

floehopper Feb 10, 2013

Member

This should be fixed in 7a0760a. It would be great if you could test against Mocha HEAD and tell me whether this fixes your problem. Thanks, James.

Member

floehopper commented Feb 10, 2013

This should be fixed in 7a0760a. It would be great if you could test against Mocha HEAD and tell me whether this fixes your problem. Thanks, James.

@floehopper

This comment has been minimized.

Show comment
Hide comment
@floehopper

floehopper Mar 7, 2013

Member

This has now been released in Mocha 0.13.3. It would be great if you could check that it solves your problem.

Member

floehopper commented Mar 7, 2013

This has now been released in Mocha 0.13.3. It would be great if you could check that it solves your problem.

@camski

This comment has been minimized.

Show comment
Hide comment
@camski

camski Mar 9, 2013

Yup, it looks like that's working fine now. Thanks!

On Thu, Mar 7, 2013 at 9:35 AM, James Mead notifications@github.com wrote:

This has now been released in Mocha 0.13.3. It would be great if you could
check that it solves your problem.


Reply to this email directly or view it on GitHubhttps://github.com/freerange/mocha/issues/134#issuecomment-14574631
.

camski commented Mar 9, 2013

Yup, it looks like that's working fine now. Thanks!

On Thu, Mar 7, 2013 at 9:35 AM, James Mead notifications@github.com wrote:

This has now been released in Mocha 0.13.3. It would be great if you could
check that it solves your problem.


Reply to this email directly or view it on GitHubhttps://github.com/freerange/mocha/issues/134#issuecomment-14574631
.

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