[rspec-devel] huge setup methods? I think I'm on the wrong path

Jens-Christian Fischer jcfischer.lists at gmail.com
Thu Jan 25 01:05:23 EST 2007


> 1. Keep the Demeterites happy
> 2. Make code easier to read
> 3. Make code easier to spec
>
> A simple example:
>
> class Client < ActiveRecord::Base
>    has_many :people
> end
>
> class Person < ActiveRecord::Base
>    belongs_to :person
>
>    def client_name
>      client.name
>    end
> end
> [...}
> You can just do:
>
> person = mock('person')
> person.stub!(:client_name).and_return('fred')

Ahh - I have been using this a lot in an other application. Thanks  
for providing some background information on *why* I have been doing  
this!

> This does mean a small amount of extra code in your app, but really
> not too much, and probably less than you'd be adding to your specs to
> stub around it.

True. Compared to the stubbing, it's nothing!

>
> We wrote a plugin that will create some of these for you
> automatically that wrap Rails' association proxies. It's part of a
> set of other convenient tools and Rails 'hacks' (hello Pot, this is
> Kettle).

I will check that out - sounds useful!

> Goldilocks sense of the word, but it works for now and saves lots o'
> lines.
>
> context "A person completing a task"
>    setup do
>      mock_model(:client, :name => 'Heys')
>      mock_model(:person, :client => @campaign, :first_name =>
> "Joe", :tasks => [])
>    end
> # ...
> end

That looks extremely helpful as well. Thanks for pointing these  
things out.

While BDD seems so simple in the beginning, it's the nitty gritty  
that makes for some headscratching.

thanks
jc


More information about the rspec-devel mailing list