Jonathan,<br><br>Thanks a lot - that accomplished what I wanted to do, and provided me with insight.<br><br>I&#39;m still having some sort of misunderstanding though, because I&#39;m having an error like this now (in a different section though; this time regarding post:
<br><br>2)<br>Spec::Mocks::MockExpectationError in &#39;MessagesController handling POST /messages should create a new message&#39;<br>Mock &#39;Message_1026&#39; received unexpected message :author_id= with (202018)<br>/Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/messages_controller.rb:31:in `create&#39;
<br>/Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in `process_without_test&#39;<br>./spec/controllers/messages_controller_spec.rb:206:in `do_post&#39;<br>./spec/controllers/messages_controller_spec.rb:211:
<br><br>yet I&#39;ve tried both Message.stub!(:author_id).and_return(1) and @message.stub!(:author_id).and_return(1) why would it still fail after stubbing both possibilities?<br><br>In my controller i&#39;m calling @message.author_id
 = current_user.id<br><br>Thanks!<br><br><div><span class="gmail_quote">On 6/2/07, <b class="gmail_sendername">Jonathan Linowes</b> &lt;<a href="mailto:jonathan@parkerhill.com">jonathan@parkerhill.com</a>&gt; wrote:</span>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">fyi, i&#39;m finding the should_receive(:messages) to be too brittle<br>because any time I add a call like @
user.messages.find or whatever it<br>breaks my spec (requiring I do .twice or .times(N) . IMO the spec<br>shouldn&#39;t be so sensitive to implementation of the behavior, so i&#39;m<br>now stubbing the association<br><br>
replaced<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.should_receive(:messages).and_return(@messages)<br>with<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.stub!(:messages).and_return(@messages)<br><br><br><br>On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote:<br><br>&gt; I&#39;m nearly as new at this as you are I&#39;m I&#39;m just figuring things out
<br>&gt; myself too.<br>&gt;<br>&gt; I think your problem is method :messages is being called on an<br>&gt; instance of User, not the class, so you need<br>&gt;<br>&gt; @user.stub!(:messages).and_return([@message])<br>&gt;
<br>&gt; here&#39;s some clues to how I&#39;ve gotten similar things working<br>&gt; To diagnose problems with my examples, I<br>&gt; - watch the test.log file. You could use &#39;tail&#39; or on mac use the<br>&gt; &#39;console&#39; utility
<br>&gt; - use the -e option in spec to run one example at a time. I copy/<br>&gt; paste the test name from the terminal window to avoid a lot of typing<br>&gt; - sometimes I comment out code in my app methods to limit what its
<br>&gt; actually doing and then add one line at a time to &quot;re-develop&quot; the<br>&gt; method<br>&gt; - similarly I may create an extra test method in my controller or<br>&gt; model to isolate things I dont understand between the spec and the
<br>&gt; code.<br>&gt;<br>&gt; I also tend to use should_receive rather than stubs, it provides more<br>&gt; details (although seems to make the tests more brittle)<br>&gt;<br>&gt; thus,<br>&gt;<br>&gt; # separate for reuse with multiple describe cases
<br>&gt; def setup_messages do<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;@user = mock_model(User)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;@message = mock_model(Message)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;@messages = [@message]<br>&gt; end<br>&gt;<br>&gt; def before(:each) do<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;setup_messages<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;User.should_receive(:find).with(&quot;1&quot;).and_return(@user)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;@user.should_receive(:messages).and_return(@messages)<br>&gt; end<br>&gt;<br>&gt; def do_get<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;get :index, :user_id =&gt; 1<br>&gt; end
<br>&gt;<br>&gt; it &quot;should find all messages&quot; do<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;do_get<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;assigns[:messages].should == @messages<br>&gt; end<br>&gt;<br>&gt; I think this is right. Please let me know<br>&gt; :)<br>&gt;<br>&gt;
<br>&gt; On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote:<br>&gt;<br>&gt;&gt; Hey,<br>&gt;&gt;<br>&gt;&gt; Sorry for so many questions - I&#39;m really bad at this right now.<br>&gt;&gt;<br>&gt;&gt; I&#39;m trying to cover the following code w/ rspec
<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; def index<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; if params[:user_id]<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @user = User.find(params[:user_id])<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @messages = @user.messages<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; end<br>&gt;&gt;&nbsp;&nbsp; end<br>&gt;&gt;
<br>&gt;&gt; So basically what I&#39;m doing is listing all the messages for a user,<br>&gt;&gt; provided there is an id parameter.<br>&gt;&gt;<br>&gt;&gt; describe MessagesController, &quot; handling GET /messages for a user&quot; do
<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; before do<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; @message = mock_model(Message)<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; @message.stub!(:user_id).and_return(1)<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; @user = mock_model(User)<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; @user.stub!(:id).and_return(1)
<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; User.stub!(:messages).and_return([@message])<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; User.stub!(:find).and_return([@user])<br>&gt;&gt;&nbsp;&nbsp; end<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; def do_get<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; get :index, :user_id =&gt; 1<br>&gt;&gt;&nbsp;&nbsp; end
<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; it &quot;should be successful&quot; do<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; response.should be_success<br>&gt;&gt;&nbsp;&nbsp; end<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; it &quot;should render index template&quot; do
<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; response.should render_template(&#39;index&#39;)<br>&gt;&gt;&nbsp;&nbsp; end<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; it &quot;should find all messages&quot; do<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; User.should_receive(:messages).and_return([@message])
<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&gt;&gt;&nbsp;&nbsp; end<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; it &quot;should assign the found messages for the view&quot; do<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; assigns[:messages].should == [@message]<br>&gt;&gt;&nbsp;&nbsp; end
<br>&gt;&gt; end<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; I&#39;m trying to use the basic scaffold spec, but I&#39;m absolutely<br>&gt;&gt; clueless<br>&gt;&gt; on what the proper way to handle this is, I&#39;m not even sure the
<br>&gt;&gt; proper<br>&gt;&gt; way I should mock the messages method, so it&#39;ll return a &quot;stub?&quot; of a<br>&gt;&gt; collection, or whatever the proper term is.<br>&gt;&gt;<br>&gt;&gt; I&#39;m sorry you have to deal with a newb, but if you could really help
<br>&gt;&gt; me out I&#39;d appreciate it a ton, thanks!<br>&gt;&gt; --<br>&gt;&gt; -Daniel Fischer<br>&gt;&gt; _______________________________________________<br>&gt;&gt; rspec-users mailing list<br>&gt;&gt; <a href="mailto:rspec-users@rubyforge.org">
rspec-users@rubyforge.org</a><br>&gt;&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt;<br>&gt; _______________________________________________<br>
&gt; rspec-users mailing list<br>&gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users
</a><br><br>_______________________________________________<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">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br><br clear="all"><br>-- <br>-Daniel Fischer<br><br><a href="http://danielfischer.com">http://danielfischer.com</a> - Geek Blog<br><a href="http://abigfisch.com">
http://abigfisch.com</a> - Portfolio<br><a href="http://writersbeat.com">http://writersbeat.com</a> - Writing Community