<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:garamond,new york,times,serif;font-size:12pt">Thanks, Jarkko.&nbsp; Substruct is not my code, so I don't know how OrderHelper got to be so big.&nbsp; I'm not sure I want to refactor its contents into Order just to make it easier to write the examples for my addition, but at least I have that option because the project is open source!<br><br>I'll have a detailed look at your suggestions later today, hopefully.&nbsp; Rails programming is not my day job, unfortunately!<br><br><br>Al<div><div style="font-family: garamond,new york,times,serif; font-size: 12pt;"><br><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">----- Original Message ----<br>From: Jarkko Laine &lt;jarkko@jlaine.net&gt;<br>To: rspec-users &lt;rspec-users@rubyforge.org&gt;<br>Sent: Tuesday, November 20, 2007 12:04:13 AM<br>Subject: Re: [rspec-users]
 confirming that a model instance was correctly created from POST params<br><br>
<br>On 20.11.2007, at 8.00, Al Chou wrote:<br>&gt; What I am trying to demonstrate to myself is that the addition of a&nbsp; <br>&gt; boolean "is_business_address" column to the order_addresses table&nbsp; <br>&gt; via a migration allows line 40 of the create_order_from_post method&nbsp; <br>&gt; to set this property when an OrderAddress instance is created.&nbsp; But&nbsp; <br>&gt; my example as shown tells me that assigns[:billing_address] is nil&nbsp; <br>&gt; when I run it.&nbsp; Also, I realize that line 16 of the example isn't&nbsp; <br>&gt; returning the right thing; it should be an AssociationCollection of&nbsp; <br>&gt; OrderAddress.&nbsp; That's where I'm starting to wonder if I'm spending&nbsp; <br>&gt; too much time on something that is probably simple enough that it's&nbsp; <br>&gt; already working but disproportionately hard to write an RSpec&nbsp; <br>&gt; example for (because the existing create_order_from_post method has&nbsp; <br>&gt;
 no tests or examples and thus isn't easy to test -- witness the 32&nbsp; <br>&gt; lines, modulo blank lines, of stubbing just to get the example to&nbsp; <br>&gt; run).<br>&gt;<br>&gt; Suggestions on how to write the example, or advice on whether to&nbsp; <br>&gt; continue to try, are requested.<br><br>A couple of points:<br><br>* I have a similar situation with shipping and billing addresses. I&nbsp; <br>solved it by adding attr_accessor :single_address to the customer&nbsp; <br>model. That gives you the option of using "check_box :single_address"&nbsp; <br>in the form for the customer and it will automatically be passed to&nbsp; <br>the model when you update its attributes. I've noticed it cleans up&nbsp; <br>the controller code a lot.<br>* for line 16 in the spec, you need to do something like this:<br><br>order_addresses = []<br>order_addresses.stub!(:create).and_return(order_address) #&nbsp; <br>order_address must be created before
 this<br>order_user.stub!( :order_addresses ).and_return( order_addresses )<br><br>* You should put all the stubbing and mocking in a before(:each)&nbsp; <br>block, they don't really belong to the actual spec.<br>* What you have in your helper is basically business domain stuff.&nbsp; <br>Helpers in Rails are meant for streamlining view code. I just&nbsp; <br>refactored some code that does similar things that yours does, and&nbsp; <br>noticed that it fits perfectly in the model. So in your case, you&nbsp; <br>could add e.g. a class method Order.create_with_addresses_and_user <br>(params[:order], params[:order_user], params[:billing_address], params <br>[:shipping_address], params[:order_account]). In that case, all you'd&nbsp; <br>have to spec in the controller is that the class method is called&nbsp; <br>with correct params. All the other spec'ing would go into the model's&nbsp; <br>specs.<br>* Unless your db supports nested transactions (very few do),
 there's&nbsp; <br>no point in putting nested transaction blocks in your code.<br>* You can shorten your mocks a bit by defining the stubbed methods&nbsp; <br>already in the mock_model call:<br><br>order_account = mock_model( OrderAccount, :valid? =&gt; true,<br>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; :order_id= =&gt; true,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; :save! =&gt; true )<br><br>and so on. Also, once your specs are more than just the one, you&nbsp; <br>start finding patterns there and you can easily refactor the stubbing&nbsp; <br>into spec helpers so that you only need a line or two to create a&nbsp;
 <br>comprehensive mock model in your specs.<br><br>HTH,<br>//jarkko<br><br>P.S. in Ruby true and false are normally written in lower case.<br><br>--<br>Jarkko Laine<br><a href="http://jlaine.net" target="_blank">http://jlaine.net</a><br><a href="http://dotherightthing.com" target="_blank">http://dotherightthing.com</a><br><a href="http://www.railsecommerce.com" target="_blank">http://www.railsecommerce.com</a><br><a href="http://odesign.fi" target="_blank">http://odesign.fi</a><br><br><br>_______________________________________________<br>rspec-users mailing list<br><a ymailto="mailto:rspec-users@rubyforge.org" 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></div></div></div></div><br>
      <hr size=1>Get easy, one-click access to your favorites. 
<a href="http://us.rd.yahoo.com/evt=51443/*http://www.yahoo.com/r/hs"> Make Yahoo! your homepage.</a>

</body></html>