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

Britt Mileshosky mileshosky at hotmail.com
Mon Jul 7 17:55:50 EDT 2008



________________________________
> 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
> 
> ---
> 
> 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) }
>       else
>         wants.html { render :action => "new" }
>       end
>     end
>   end
> 
> 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


Where in your create action are you defining @organization? is this variable provided from a before filter? If not, that's the first thing you should look at.  Another thing I noticed is that your @mock_note defines its organization @mock_org before @mock_org is defined... thus probably setting @mock_note.organization to nil.

_________________________________________________________________
The i’m Talkaton. Can 30-days of conversation change the world?
http://www.imtalkathon.com/?source=EML_WLH_Talkathon_ChangeWorld


More information about the rspec-users mailing list