[rspec-users] Best practice thoughts: Model helpers, mocks

Matthijs Langenberg mlangenberg at gmail.com
Thu Apr 19 04:04:06 EDT 2007


I guess you are using integrated views, aren't you? Focus on the
behaviour of your controller method, I don't think it would ever need
all those stubbed fields, but please prove me wrong.

On 4/19/07, Courtenay <court3nay at gmail.com> wrote:
> >   setup do
> >     mem_types = Array.new
> >     @membership_type1 = mock_model(MembershipType)
> >     @membership_type1.stub!(:id).and_return(1)
> >     @membership_type1.stub!(:name).and_return('Name')
> >     @membership_type1.stub!(:price_cents).and_return(5000)
>
> this is a little shorter:
>
>     @membership_type1 = mock_model(MembershipType, :id => 1, :name =>
> 'Name', :price_cents => 5000
>
> also, some questions;
>
> Are you really using all those fields in all your specs? if so, looks
> like some of your code can be pushed to the model.  There's rarely a
> need to define all those stubs, unless of course you have some killer
> logic.  Stub out the fields you are doing logic with, and forget the
> rest.
>
> Finally, check out
>
> http://blog.caboo.se/articles/2007/4/16/dynamic-rspec-setup-methods
>
> as a way to modularize your setup methods.
>
> I am still learning this stuff myself, but a controller setup in my
> experience usually runs to just a few lines.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list