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

Tiffani Ashley Bell tiffani2k3 at gmail.com
Tue Jul 8 00:14:06 EDT 2008


Yeah, I was on 1.1.4 and this solution with a few tweaks definitely worked.
The original set up of my code called for having the Organization returned
by the notes controller in a before filter, so I stubbed out this stuff in
the before(:each) block.  It ended up looking like this:

before(:each) do
   @organization = stub_model(Organization, :name => "Slappy's Hot Dog
Palace")
   Organization.stub!(:find).and_return(@organization)
end

Then, I followed Craig's lead on what the example should do with a few
things taken out:

it "should render 'notes/new' when an empty note is submitted" do
  Note.stub!(:new).and_return(stub("note"))

  post :create, :appointment_id => @appointment.id, :organization_id => @
organization.id, :new_note => { :body => "" }
  response.should render_template("notes/new")
end

I kept the controller method just like it was in the original post.  And
whoooo!  Everything passed.  I'll be making sure I really differentiate the
necessity of mock_model vs. stub_model from now on, too as a take-away from
all this.  Learning this stuff has really been interesting considering
months ago the only testing I was doing was making changes in Textmate and
then clicking "Refresh" in a browser. lol.

Thanks again!
Tiffani AB

On Mon, Jul 7, 2008 at 10:31 PM, Craig Demyanovich <cdemyanovich at gmail.com>
wrote:

> I'm assuming RSpec 1.1.3+, but here's how I might write this example:
>
> describe NotesController do
>  it "renders 'notes/new' when an empty note is submitted" do
>    Note.stub!(:new).and_return(stub("note"))
>    organization = stub_model(Organization, :notes => stub("notes",
> :<< => false))
>    assigns[:organization] = organization
>
>    post :create, "organization_id" => organization.to_param,
> "new_note" => { "body" => "" }
>
>    response.should render_template("notes/new")
>  end
> end
>
> I'm favoring the use of stub_model and stubs instead of mock_model
> (and mocks) because we're not setting any expectations on the note or
> the organization. Rather, we're just setting them up to deliver values
> that are either simply required (as in Note.new returning something
> non-nil) or what we want (the addition of the note to the organization
> to fail).
>
> I've also eliminated the idea of having existing notes on the
> organization. It's not central to what's being spec'd, so it's out.
>
> Furthermore, I made the submitted note's body empty because that's
> what was in the original description of the it block. That those two
> didn't match bothered me.
>
> Finally, I simplified the description of the it block to what I
> thought the essence of your example was; I hope that I'm at least
> close. :-)
>
> Please let the list know if this helped or if you already revisited
> the problem and solved it. I think we're interested in what ends up
> working for you.
>
> Regards,
> Craig
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080708/d737da41/attachment-0001.html>


More information about the rspec-users mailing list