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