0

I know that I can mock objects as follows:

Account.any_instance.expects(:type).returns("premium")

I remember hearing that this is considered a poor practice. I'd love to do something like

user = users(:bob)
user.account.expects(:type).returns("premium")

but this doesn't seem to mock the right object. Is there a better solution than using any_instance call on the class?

Edit: the error message that I'm getting is

not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Account:0x5f9f8f8>.type(any_parameters)
Vega
  • 27,856
  • 27
  • 95
  • 103
sakovias
  • 1,356
  • 1
  • 17
  • 26
  • I think it would be even better if you show the whole method and the whole test. Right now it can only be a matter of guessing. – sebkkom Oct 08 '14 at 14:56

1 Answers1

0

I am still finding my way with mocks but have you tried something like:

account = user.account
account.expects(:type).returns("premium")

It would help to have the context of your test to know which object you wanted mocked.

sebkkom
  • 1,426
  • 17
  • 31
  • Just tried that, thanks. I'm getting the same error message which I'll add to my question in a bit. – sakovias Oct 08 '14 at 14:43