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

Craig Demyanovich cdemyanovich at gmail.com
Mon Jul 7 22:31:27 EDT 2008

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
    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")

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.


More information about the rspec-users mailing list