[rspec-users] Problem testing method
matt at mattwynne.net
Thu Sep 25 10:06:22 EDT 2008
> What way it's better to test this method? Using a real instance of
> or using a mock?
It basically depends on how complex Game is. In this simplistic
example, there's really no harm in testing both objects together at
the same time, but nine times out of ten in the real world, you want
to isolate your tests so that when a bug is introduced in Game it's
only the specs for Game that fail, and not the specs for anything else.
Mocks help with this by putting 'firewalls' between the classes
you're testing, so you can be sure that you're only testing a single
class at once. Once you get the hang of them, they can also help you
to think a little more about the interactions between your classes,
and produce cleaner designs. The trade-off is that you need to keep
the mocked behaviour consistent with the actual behaviour of the
class being mocked.
It sounds as though you could benefit from reading about mocks. Check
out the links at the bottom of this page:
In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.
More information about the rspec-users