Thanks David and Ben for the comprehensive replies! I really appreciate it :)

These wikipedia articles helped me to understand on the more conceptual level:

http://en.wikipedia.org/wiki/Method_stub
http://en.wikipedia.org/wiki/Mock_object

So, if I understand it right -- Stub is for methods and Mocks are for
roles (or, in the technical-level -- objects).

In the context of rSpec/rSpec-Rails, however,  the API makes it
confusing, to be honest. Being able to create a Mock with
Spec::Mocks:Mock with Spec::Mocks::ExampleMethods# confuses me.

The same applies for stub_model() and mock_model(). I know that
mock_model is a convenience method that can be used to create an
object with an API similar to ActiveRecord::Base's. The API says
stub_model is basically the same, but prevents the object from
accessing the database. Ok, but why attaching these concepts to Stub?
Is this the right name to use for this method? (Stub == mock object
that is not allowed to access the database?).

And there's the  Spec::Mocks::Methods#stub method, which is the one
that reflects the concept that "the Stub is a dummy method".

So, in sum, my perception is that rSpec/rSpec-Rails implement all the
functionality needed to implement Mocks and Stubs,  but in a confusing
way. IMHO, the API is not succinct in this aspect -- it does not
communicate a clear distinction between Mocks and Stubs and this makes
it hard for someone new to TDD/BDD to use them in the right way or
decide the best way to use them.

My 0.4 cents of Brazilian Reals.

@David -- yes, I'm Brazilian! You've got a good gut! And I'm glad to
see that you know Portuguese! :)

Marcelo.

On Thu, Jul 23, 2009 at 8:47 PM, David Chelimsky<dchelim...@gmail.com> wrote:
> On Jul 23, 7:41 pm, Ben Mabey <b...@benmabey.com> wrote:
>> Marcelo de Moraes Serpa wrote:> Hello list,
>>
>> > >From what I could see, the lines between mocks and stubs are subtle,
>> > but the general idea I got is that mocks set expectations and stubs
>> > are only dummy objects/method calls. What confused me though, is that
>> > the stub() method is an alias for mock() in
>> > Spec::Mocks::ExampleMethods. So a stub is a mock?
>>
>> Yes, no, and maybe so. :p  I agree with your general understanding that
>> "mocks set expectations and stubs are only dummy objects/method calls".
>> The fact that rspec using the same machinery to provide stubs and mocks
>> is really inconsequential because the deciding factor of what they are
>> is how you use them.  You can, IMO, use a "mock" like a stub and a
>> "stub" like a mock.. Take this for example:
>>
>> describe "#some_method" do
>>   it "delegates to some_obejct" do
>>     some_object = stub('some object', :some_method => "foo")
>>     my_object = MyObject.new(some_object)
>>
>>     my_object.some_method.should == "foo"
>>   end
>> end
>>
>> We are using a stub as a dummy object, and yet our expectation is
>> clearing testing the method call.  So is the above a stub or is it
>> really a mock?  I would say that it is acting like a mock.  I hope that
>> others on the list will correct me if I am wrong making this
>> observation/conclusion.   Likewise we could use a mock as a dummy object
>> and never set any expectations on it and it would be acting like a
>> stub.  Even though stub and mock is the same in RSpec you should still
>> use the correct name when using them.
>>
>> > Also, another thing that is confusing: You have stub and stub! methods
>> > in the Spec::Mocks::Methods namespace, what is the difference between
>> > Spec::Mocks::ExampleMethods#stub and Spec::Mocks::Methods#stub ?
>>
>> Spec::Mocks::ExampleMethods are the methods that you can call during the 
>> course of a code example.  So when you say stub("something") the method in 
>> Spec::Mocks::ExampleMethods gets called and returns you a stub.  Now, that 
>> stub object now has it's own #stub method which lives on 
>> Spec::Mocks::Methods#stub.  That call will stub out the provided method and 
>> return value... So my_stub.stub(:foo, "bar"), however that is aliased from 
>> #stub! which is used most often.  This is all from memory so I could be 
>> wrong but that is the general gist of it.
>>
>> HTH,
>> -Ben
>
> Ben and I are secretly the same person.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to