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

Ashley Moran ashley.moran at patchspace.co.uk
Sun May 18 05:13:03 EDT 2008


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.



More information about the rspec-users mailing list