Skip to content
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 attr_accessor #196

Closed
dvlscm opened this issue Apr 6, 2014 · 3 comments
Closed

Can't stub attr_accessor #196

dvlscm opened this issue Apr 6, 2014 · 3 comments
Assignees

Comments

@dvlscm
Copy link

dvlscm commented Apr 6, 2014

I'm new to Ruby and Mocha, so what I'm about to report could be user error. If so, please let me know.

I'm using minitest. Stubbing an attr_accessor for any_instances appears to have no effect at all. A small, contrived example:

class Order
  attr_accessor :shipping_period

  def shipping_delay
    return 5 + @shipping_period
  end
end

require 'minitest/autorun'
require 'mocha/mini_test'

class TestOrder < Minitest::Test
  def test_shipping_delay
    order = Order.new
    Order.any_instance.stubs(:shipping_period).returns(7)
    assert_equal 12, order.shipping_delay
  end
end

That gives me this result:

  1. Error:
    TestOrder#test_shipping_delay:
    TypeError: nil can't be coerced into Fixnum
    tester.rb:60:in +' tester.rb:60:inshipping_delay'
    tester.rb:71:in `test_shipping_delay'

Apparently the stub didn't actually set things up such that @shipping_period has the value 7. The Partial Mocking example on the main page for Mocha shows something similar with stubbing an attr_accessor (the last test), so it doesn't seem like I'm doing something technically out of bounds.

I'm sure there are reasons why stubbing an attr_accessor could be considered wrong, or unadvisable, or not ideal. Setting those aside, am I doing something technically wrong to stub an attr_accessor, or is this an issue in Mocha?

Roy

@floehopper
Copy link
Member

I'm sorry for the slow response - I've only just seen this, because my GitHub email notifications for this repo stopped working for some reason. I'll try to take a look at this as soon as I can. Cheers, James.

@floehopper
Copy link
Member

The problem in your example is that Order#shipping_delay is using the @shipping_period instance variable directly i.e. it is not calling the accessor "getter" method Order#shipping_period. If you remove the @ sign, I think your test will pass.

I'm going to close this issue for now, but feel free to re-open it if I haven't answered your question. If you have more questions about how to use Mocha, it might be better to use the mailing list.

@dvlscm
Copy link
Author

dvlscm commented Jun 19, 2014

Thanks, James. I believe you're right. Silly mistake on my part there. I'll try your suggestion. Odds are good it'll be a-okay. And thanks for the mailing list pointer. I'll ask future usage questions over there.

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

No branches or pull requests

2 participants