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:<br>
<br>before(:each) do<br> @organization = stub_model(Organization, :name => "Slappy's Hot Dog Palace")<br> Organization.stub!(:find).and_return(@organization)<br>end<br><br>Then, I followed Craig's lead on what the example should do with a few things taken out:<br>
<br>it "should render 'notes/new' when an empty note is submitted" do<br> Note.stub!(:new).and_return(stub("note"))<br><br> post :create, :appointment_id => @<a href="http://appointment.id">appointment.id</a>, :organization_id => @<a href="http://organization.id">organization.id</a>, :new_note => { :body => "" }<br>
response.should render_template("notes/new")<br>end<br><br>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. <br>
<br>Thanks again!<br>Tiffani AB<br><br><div class="gmail_quote">On Mon, Jul 7, 2008 at 10:31 PM, Craig Demyanovich <<a href="mailto:cdemyanovich@gmail.com">cdemyanovich@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm assuming RSpec 1.1.3+, but here's how I might write this example:<br>
<br>
describe NotesController do<br>
it "renders 'notes/new' when an empty note is submitted" do<br>
Note.stub!(:new).and_return(stub("note"))<br>
organization = stub_model(Organization, :notes => stub("notes",<br>
:<< => false))<br>
assigns[:organization] = organization<br>
<br>
post :create, "organization_id" => organization.to_param,<br>
"new_note" => { "body" => "" }<br>
<div class="Ih2E3d"><br>
response.should render_template("notes/new")<br>
end<br>
</div>end<br>
<br>
I'm favoring the use of stub_model and stubs instead of mock_model<br>
(and mocks) because we're not setting any expectations on the note or<br>
the organization. Rather, we're just setting them up to deliver values<br>
that are either simply required (as in Note.new returning something<br>
non-nil) or what we want (the addition of the note to the organization<br>
to fail).<br>
<br>
I've also eliminated the idea of having existing notes on the<br>
organization. It's not central to what's being spec'd, so it's out.<br>
<br>
Furthermore, I made the submitted note's body empty because that's<br>
what was in the original description of the it block. That those two<br>
didn't match bothered me.<br>
<br>
Finally, I simplified the description of the it block to what I<br>
thought the essence of your example was; I hope that I'm at least<br>
close. :-)<br>
<br>
Please let the list know if this helped or if you already revisited<br>
the problem and solved it. I think we're interested in what ends up<br>
working for you.<br>
<br>
Regards,<br>
<font color="#888888">Craig<br>
</font><div><div></div><div class="Wj3C7c">_______________________________________________<br>
rspec-users mailing list<br>
<a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>
<a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
</div></div></blockquote></div><br>