On Fri, Jul 4, 2008 at 8:32 AM, Tiffani Ashley Bell
<[EMAIL PROTECTED]> wrote:
> Hi everybody,

Hi Tiffany, welcome to Rspec

> I was reading the Typo source code, however, and came across some code that
> I didn't know exactly how it worked.  I've noticed that in testing one of
> their controllers, they use a variable (@comments) that they don't declare
> anywhere else, yet they use it as a stand in for collections on some of the
> mocks.  How is that possible?  I know in the mocking documentation it says
> that you can define collaborations with other objects before those objects
> exist, but how is that working in this code?  I only ask that because later,
> you see code like this:  @comments.stub!(:build).and_return(@comment).

If you have a look at the descriptions, they use :shared => true.
This is a way of being DRY in RSpec (which I personally don't think is
such a good idea).

What the shared => true declaration allows you to do is to include
that block of code elsewhere with 'it should behave like my shared
code'

So we have (describe "All Requests", :shared => true do)

and then the next description block is:

describe "General Comment Creation", :shared => true do
  it_should_behave_like "All Requests"

Which then includes the All Requests block (which is just a before method).

The @comments variable gets declared in:

@comments.stub!(:build).and_return(@comment)

and then this is tied in to the Article model in the _previous_ code
block like so:

    @article  = mock_model(Article,
                  :comments                   => @comments,
                  :published_comments         => @comments,
                  :add_comment                => @comment)


So when you call @article.comments you get @comments as a stub back
which stubs :build and returns a @comment.

Ugh.

This is where, in RSpec, you can dig a very fast grave.  Because
you'll come back to this code in 6-12 months and be totally stuck
trying to figure out what is where.

I recently wrote a viewpoint on this that might help you:
http://www.lindsaar.net/2008/6/24/tip-24-being-clever-in-specs-is-for-dummies

Hope you do well with Rspec, feel free to ask more questions!

-- 
http://lindsaar.net/
Rails, RSpec, Puppet and Life blog....
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to