I get the unexpected message :user= failure.<br><br>When you set up the mock with the initial parameters, wouldn&#39;t those be stubbing: @coupon.stub!(:user).and_return(@current_user)<br><br>While, my spec says that I want :user= to be called?
<br><br>-corey<br><br><div class="gmail_quote">On Jan 23, 2008 10:35 AM, David Chelimsky &lt;<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On Jan 23, 2008 9:24 AM, Corey Haines &lt;<a href="mailto:coreyhaines@gmail.com">coreyhaines@gmail.com</a>&gt; wrote:<br>&gt; Thanks, David!<br>&gt;<br>&gt; Here&#39;s what I morphed the specs into:<br>
&gt;<br>&gt; <a href="http://pastie.caboo.se/142411" target="_blank">http://pastie.caboo.se/142411</a><br>&gt;<br>&gt; And, I ended up with a blog entry that I&#39;ll write tonight.<br>&gt;<br>&gt; &nbsp;Basically, here&#39;s the situation I&#39;ve been running into which is causing my
<br>&gt; specs to grow.<br>&gt;<br>&gt; If I don&#39;t set up something to tell the coupon that it is going to have<br>&gt; :user= called on it, then the other tests fails. I didn&#39;t want to stub it,<br>&gt; as I want this to be part of my expectations. Luckily, it only took a minute
<br>&gt; to come up with a possible solution, again relying on the fact that things<br>&gt; tend to work how I think they should.<br>&gt;<br>&gt; The solution I thought of was to set the stub! call in the before(:each),<br>
&gt; then set an actual expectation in the spec.<br><br></div>Actually - that&#39;s what mine did to. These two are the same:<br><br>coupon = mock_model(Coupon,<br> &nbsp; &nbsp; &nbsp;:user= =&gt; nil,<br> &nbsp; &nbsp; &nbsp;:save =&gt; true<br> &nbsp; &nbsp;)
<br><br>coupon = mock_model Coupon<br>coupon.stub!(:user=)<br>coupon.stub!(:save).and_return(true)<br><br>:)<br><br>Looking forward to your blog.<br><br>Cheers,<br><font color="#888888">David<br></font><div><div></div><div class="Wj3C7c">
<br>&gt; In &nbsp;my mind, the expectation<br>&gt; should override the stub (making sure it gets called), but the stub will<br>&gt; allow the other tests to pass. Fantastic! It works. I&#39;m going to write up a<br>&gt; blog entry on this pattern, and I&#39;ll send a link when I post it.
<br>&gt;<br>&gt; Thanks for your help.<br>&gt; -Corey<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt; On Jan 23, 2008 10:03 AM, David Chelimsky &lt;<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>&gt; wrote:
<br>&gt; &gt;<br>&gt; &gt; On Jan 23, 2008 8:49 AM, Corey Haines &lt;<a href="mailto:coreyhaines@gmail.com">coreyhaines@gmail.com</a>&gt; wrote:<br>&gt; &gt; &gt; Of course. Thanks, David! I still am getting used to user=, rather than
<br>&gt; just<br>&gt; &gt; &gt; user. Thanks again.<br>&gt; &gt;<br>&gt; &gt; No problem. I certainly got caught by that early on.<br>&gt; &gt;<br>&gt; &gt; I have some more general comments. See below:<br>&gt; &gt;<br>&gt; &gt;
<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; -Corey<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; On Jan 23, 2008 9:46 AM, David Chelimsky &lt; <a href="mailto:dchelimsky@gmail.com">
dchelimsky@gmail.com</a>&gt; wrote:<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; On Jan 23, 2008 8:41 AM, Corey Haines &lt; <a href="mailto:coreyhaines@gmail.com">coreyhaines@gmail.com</a>&gt; wrote:<br>&gt; &gt; &gt; &gt;
<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; All,<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; I&#39;m missing something simple, I think. I am writing a spec to say<br>&gt; that<br>
&gt; &gt; &gt; my<br>&gt; &gt; &gt; &gt; &gt; CouponController should create a new coupon from the form<br>&gt; parameters,<br>&gt; &gt; &gt; then<br>&gt; &gt; &gt; &gt; &gt; set the current user. Here&#39;s the spec:<br>
&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; describe CouponController, &quot;When posting to save_coupon&quot; do<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; &nbsp; before(:each) do<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; @expectedName = &quot;pepper&#39;s&quot;
<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; @expectedAmount = 5<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; coupon = mock_model Coupon<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; current_user = mock_model User<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; 
controller.stub! (:current_user).and_return(current_user)<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; Coupon.should_receive(:new).with({&quot;name&quot;=&gt;@expectedName,&quot;amount&quot;=&gt;@expectedAmount}).and_return(coupon)
<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; coupon.should_receive(:user).with(current_user)<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; coupon.should_receive(:save)<br>&gt; &gt; &gt; &gt; &gt; &nbsp; end<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; &nbsp; it &quot;should tell the Coupon model to create a new coupon with the
<br>&gt; given<br>&gt; &gt; &gt; &gt; &gt; parameters and save&quot; do<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; post<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &#39;save_coupon&#39;,{:coupon=&gt;{:name=&gt;@expectedName,:amount=&gt;@expectedAmount}}
<br>&gt; &gt; &gt; &gt; &gt; &nbsp; end<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; Here&#39;s the controller method:<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; &nbsp; def save_coupon<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; coupon = 
Coupon.new(params[:coupon])<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; coupon.user = current_user<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; coupon.save<br>&gt; &gt; &gt; &gt; &gt; &nbsp; &nbsp; redirect_to_index &quot;Coupon Added!&quot;<br>&gt; &gt; &gt; &gt; &gt; &nbsp; end
<br>&gt; &gt;<br>&gt; &gt; This one example is doing too much IMO. You even say &quot;I am writing a<br>&gt; &gt;<br>&gt; &gt; spec to say that my CouponController should create a new coupon from<br>&gt; &gt; the form parameters, then set the current user.&quot; That&#39;s two things.
<br>&gt; &gt;<br>&gt; &gt; Generally I try to keep stubs in before(:each), expectations in the<br>&gt; &gt; examples, and one example for each concept that I&#39;m describing.<br>&gt; &gt;<br>&gt; &gt; I took the liberty of pastie-ing what I&#39;d probably do. I haven&#39;t run
<br>&gt; &gt; it, so it might not work as/is, but you&#39;ll get the idea.<br>&gt; &gt;<br>&gt; &gt; <a href="http://pastie.caboo.se/142403" target="_blank">http://pastie.caboo.se/142403</a><br>&gt; &gt;<br>&gt; &gt; Cheers,
<br>&gt; &gt; David<br>&gt; &gt;<br>&gt; &gt; PS - I&#39;m really glad to see you getting involved with this list.<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; And, I get the following failure:
<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; Mock &#39;Coupon_1008&#39; received unexpected message :user= with<br>&gt; &gt; &gt; (#&lt;User:0x221a3e8<br>&gt; &gt; &gt; &gt; &gt; @name=&quot;User_1009&quot;&gt;)
<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; That&#39;s from this line in save_coupon:<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; coupon.user = current_user<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; Just need to stub that:
<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; coupon.stub!(:user=)<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; Or you could expect it:<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; coupon.should_receive
 (:user=).with(current_user)<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; Cheers,<br>&gt; &gt; &gt; &gt; David<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; I&#39;m sure that I&#39;m missing something very simple, but I&#39;ve been
<br>&gt; staring<br>&gt; &gt; &gt; at it<br>&gt; &gt; &gt; &gt; &gt; for too long.<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; (also, if anyone has commented on my style, please feel free to<br>&gt; mention<br>
&gt; &gt; &gt; it,<br>&gt; &gt; &gt; &gt; &gt; as I&#39;m still adjusting my mind to rspec)<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; Oh, versions, I almost forgot:<br>&gt; &gt; &gt; &gt; &gt; rails 2.0.2<br>
&gt; &gt; &gt; &gt; &gt; rspec(_on_rails) plugins just updated from current a couple days<br>&gt; ago,<br>&gt; &gt; &gt; not<br>&gt; &gt; &gt; &gt; &gt; totally sure how to get the versions of the plugins<br>&gt; &gt; &gt; &gt; &gt;
<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; Thanks.<br>&gt; &gt; &gt; &gt; &gt; -Corey<br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; &gt; --<br>&gt; &gt; &gt; &gt; &gt; <a href="http://www.coreyhaines.com" target="_blank">
http://www.coreyhaines.com</a><br>&gt; &gt; &gt; &gt; &gt; The Internet&#39;s Premiere source of information about Corey Haines<br>&gt; &gt; &gt; &gt; &gt; _______________________________________________<br>&gt; &gt; &gt; &gt; &gt; rspec-users mailing list
<br>&gt; &gt; &gt; &gt; &gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; &gt; &gt; &gt; &gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users
</a><br>&gt; &gt; &gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; _______________________________________________<br>&gt; &gt; &gt; &gt; rspec-users mailing list<br>&gt; &gt; &gt; &gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org
</a><br>&gt; &gt; &gt; &gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt; &gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;
<br>&gt; &gt; &gt; --<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; <a href="http://www.coreyhaines.com" target="_blank">http://www.coreyhaines.com</a><br>&gt; &gt; &gt; The Internet&#39;s Premiere source of information about Corey Haines
<br>&gt; &gt; &gt; _______________________________________________<br>&gt; &gt; &gt; rspec-users mailing list<br>&gt; &gt; &gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; &gt; &gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt; &gt; &gt;<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" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt; &gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt; --<br>
&gt; <a href="http://www.coreyhaines.com" target="_blank">http://www.coreyhaines.com</a><br>&gt; The Internet&#39;s Premiere source of information about Corey Haines<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" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users
</a><br>&gt;<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" target="_blank">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br></div></div></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.coreyhaines.com">http://www.coreyhaines.com</a><br>The Internet&#39;s Premiere source of information about Corey Haines