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

Tiffani Ashley Bell tiffani2k3 at gmail.com
Mon Jul 7 17:27:36 EDT 2008

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]

it "should render 'notes/new' when the Note is setup with invalid data, i.e.
without a body on POST create" do
      post :create, :organization_id => @mock_org.id, :new_note => { :body
=> @mock_note.body }
      response.should render_template("notes/new")


in notes_controller.rb:

def create
    @new_note = Note.new(params[:new_note])

    respond_to do |wants|
      if @organization.notes << @new_note
        wants.html { redirect_to organization_url(@organization) }
        wants.html { render :action => "new" }

I figured that my stubbing the << method out in the test to return false
would do the job of triggering the proper branch of the if statement in the
controller action, but I'm not sure why it's not doing so.  I get the error:
expected "notes/new", got nil.  I have code in my Users controller that does
the exact same thing with the << method and the tests pass (they're
virtually identical tests).  Any thoughts?  TIA.

--Tiffani AB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080707/cff347c9/attachment.html>

More information about the rspec-users mailing list