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

Courtenay court3nay at gmail.com
Thu Apr 19 01:59:05 EDT 2007


>   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.


More information about the rspec-users mailing list