[rspec-users] Using Hash to mock classes that respond to :[]

John D. Hume duelin.markers at gmail.com
Mon May 19 12:57:53 EDT 2008


I do that sort of thing pretty often.

I would just point out that you should avoid calling that mocking, since
there are no expectations in place and therefore no verification of the
specific way the code being specified interacts with the MigrationGraph.
According to the definitions in
http://martinfowler.com/articles/mocksArentStubs.html (which are apparently
from http://xunitpatterns.com/ but I haven't read it), your hash is a 'fake'
MigrationGraph.

-hume.


On Sun, May 18, 2008 at 5:13 AM, Ashley Moran <ashley.moran at patchspace.co.uk>
wrote:

> Hi
>
> I've been doing this a while now.  If I have a class that responds to :[],
> and all I'm interested in stubbing is that method, then instead of using a
> mock I just create a hash.  (Similarly, you could create an array when you
> are testing an Enumerable object.)
>
> Example from code I just worked on, where uses MigrationGraph#[] looks up
> graph vertices by name:
>
>  @migration_graph = {
>    # "name" => vertex
>    "changeset 1 migration 1" => @migration_a,
>    "changeset 1 migration 2" => @migration_b,
>    "changeset 2 migration 1" => @migration_c,
>    "changeset 2 migration 2" => @migration_d
>  }
>
> What I'm really interested in is what happens to (some of) the four
> migrations, not the migration graph itself.
>
> I just wondered if anyone else did this.  Generally doing unusual things is
> a sign something is amiss.
>
> Ashley
>
>
> --
> http://www.patchspace.co.uk/
> http://aviewfromafar.net/
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080519/9022d4ff/attachment.html>


More information about the rspec-users mailing list