[rspec-users] Not sure why this controller spec isn't working

Zach Dennis zach.dennis at gmail.com
Mon Jul 7 18:06:54 EDT 2008


On Mon, Jul 7, 2008 at 5:55 PM, Britt Mileshosky <mileshosky at hotmail.com>
wrote:

>
>
> ________________________________
> > Date: Mon, 7 Jul 2008 17:27:36 -0400
> > From: tiffani2k3 at gmail.com
> > To: rspec-users at rubyforge.org
> > Subject: [rspec-users] Not sure why this controller spec isn't working
> >
> > Hey folks,
> >
> > I've been mocking and stubbing pretty nicely after the various bits of
> advice I received earlier about doing so.  I've come to bits of code that
> work in one place and not in another, though.  I'm hoping it's not something
> simple I've missed.  The code below fails even though code that is
> practically the same elsewhere (except different models in use) passes.
>  What gives?
> >
> > I have:
> >
> > in notes_controller_spec.rb:
> >
> >
> > before(:each) do
> >     @mock_note = mock_model(Note, :body => "The hot dog shipment will be
> in later tonight.",
> >                                   :organization => @mock_org)
> >     @mock_org = mock_model(Organization, :name => "Slappy's Hot Dog
> Palace", :notes => [@mock_note])
> >
> >     @notes = [@mock_note]
> >   end
> >
> >
> > it "should render 'notes/new' when the Note is setup with invalid data,
> i.e. without a body on POST create" do
> >       Note.stub!(:new).and_return(@mock_note)
> >       @notes.stub!(:<       post :create, :organization_id => @
> mock_org.id, :new_note => { :body => @mock_note.body }
> >       response.should render_template("notes/new")
> >     end
> >
>

To add to what Britt already said, look at your example and then your before
block again very carefully. Notice that you setup @notes in your before, and
you use it to stub :<< in your example, but you never return @notes from
anything. Instead you setup @mock_org to return an array with @mock_note.
Although the contents of this and @notes are the same, you have created two
different arrays. You are probably wanting to be dealing with the same
@notes object in each of these cases.

Zach


-- 
Zach Dennis
http://www.continuousthinking.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080707/2c1945f5/attachment.html>


More information about the rspec-users mailing list