[rspec-devel] [Rspec-devel] RSpec mock ideas/bugs

David Chelimsky dchelimsky at gmail.com
Mon Sep 18 00:18:25 EDT 2006

On 9/11/06, Mikkel Garcia <mikkel at shatteredruby.com> wrote:
> Hey guys,
> I'm a big fan of what y'all are trying to do.  I've been running into a few
> issues with it, in the form of bugs(non-conformity) and ideas.
> Conformity - mock objects
> Mocks have a .nil? function
> context "A mock" do
>   specify "should have no responses to any events" do
>     @object = mock(:object)
>     @object.nil?            #Will not raise a mock unexpected message event
>     @object.object_id       #Will raise a mock unexpected message event
>   end
> end
> Mocks returned by mocks expect an :instance_of call
> context "A mock returning a mock" do
>   specify "should not receive instance_of message" do
>     player = mock(:player)
>     arch_enemy = mock(:arch_enemy)
> player.should_receive(:most_hated_enemy).and_return(arch_enemy)
>     player.most_hated_enemy
>     # This mock fails with: Mock 'enemy' received unexpected message
> 'instance_of?' with [<Array:Class>]
>   end
> end

Please submit these at http://rubyforge.org/tracker/?group_id=797

> Ideas
> Mocks being substituted for real objects - True integration tests for free ?
> Alot of the specs we are writing separate responsibilities among tightly
> coupled classes.  We are testing the individuality, but if we wanted to test
> the true integration of those classes we would probably have to create new
> specs.  How about, instead, creating mock objects from classes themselves
> and having a rake spec:integrate task.  This task would substitute the real
> classes for the mocks, which brings me to:
> Mocks/stubs syntax
> Let's say I have a Level object, and that is responsible for reading my map
> into the game.  I have an object in that map I want to test(err...spec).
> Would something like this be feasible?
> @level = Level.mock
> or
> @level = Level.mock(:null_object => true)
> Or maybe I have a class that has a GUI portion, and I want to stub that
> function.
> @level = Level.stub  #Keeps all the Level functionality and lets you stub
> things out
> @level.should_receive(:draw).and_return(true)
> That way, if you do something like:
> spec -integrate level_spec.rb
> it could turn all mocks and stubs into real objects.

I think this would add a bunch of unwanted complexity to the rspec
code-base itself, as well as the specs that you'd have to write. You
would probably have to, for example, have two different setups - one
to set up the mock object and one to set up the real object.

Feel free to add this one as a feature request at rubyforge as well,
and we can continue the discussion in that forum.

> I'm really excited about the implications this has towards simplifying game
> design, and I'm hoping that it works well enough to replace Test::Unit for
> our 3d game framework, Shattered.
> Thanks,
> -Mikkel Garcia
> http://shatteredruby.com
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel

More information about the rspec-devel mailing list