<div>Been a month since I&#39;ve rspec, I think you can place the stubs here.</div><div><br class="webkit-block-placeholder"></div><div>@user = mock_model(User,</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>login =&gt; &quot;&quot;,</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>email =&gt; &quot;&quot;,</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>password<span class="Apple-tab-span" style="white-space:pre">        </span>=&gt; &quot;&quot;,</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>password_confirmation =&gt; &quot;&quot;</div><div>)</div><div><br class="webkit-block-placeholder"></div><div>@account = mock_model(Account,&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>subdomain =&gt; &quot;&quot;,</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>company_name =&gt; &quot;&quot;,</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>filename =&gt; &quot;&quot;</div><div>)</div><br><div class="gmail_quote">
On Jan 28, 2008 2:36 PM, Chris Olsen &lt;<a href="mailto:lists@ruby-forum.com">lists@ruby-forum.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
In a create controller method I am testing the else clause that occurs<br>when the model is unable to be saved. &nbsp;Sounds simple enough, but because<br>model is created before the save and the form has to be re-filled I now<br>
have to stub a model and all the attributes. &nbsp;Since the error messages<br>are displayed I also have to stub some methods within the model that<br>allow the error messages to be displayed properly.<br><br>This small test turns into a lot of code. &nbsp;So I am wondering if there is<br>
something that I am missing to make this much simpler. &nbsp;Things did start<br>simple, in the stubbing of the new method within the Account class and<br>then the save method within the @account model. &nbsp;Then I get errors<br>reminding me to stub out the users method for the account model, then<br>
the build method, etc<br><br>I have looked at the generated scaffold code and it is simpler, but it<br>seems that the relationship made between the two models is what starts<br>to make this more complex.<br><br>Thanks for the help.<br>
<br>Here is my current code:<br>===============<br> &nbsp;before :each do<br> &nbsp; &nbsp;@account = mock_model(Account)<br><br> &nbsp; &nbsp;#what is being tested here<br> &nbsp; &nbsp;@account.stub!(:save).and_return(false)<br><br> &nbsp; &nbsp;#User info<br> &nbsp; &nbsp;@user = mock_model(User)<br>
 &nbsp; &nbsp;users = mock(&quot;Userlist&quot;)<br> &nbsp; &nbsp;@account.stub!(:users).and_return(users)<br> &nbsp; &nbsp;users.stub!(:build).and_return(@user)<br><br> &nbsp; &nbsp;#since the form is repopulated the account mock must have stub for<br>all the attributes<br>
 &nbsp; &nbsp;@account.stub!(:subdomain).and_return(&quot;&quot;)<br> &nbsp; &nbsp;@account.stub!(:company_name).and_return(&quot;&quot;)<br> &nbsp; &nbsp;@account.stub!(:filename).and_return(&quot;&quot;)<br> &nbsp; &nbsp;@user.stub!(:login).and_return(&quot;&quot;)<br>
 &nbsp; &nbsp;@user.stub!(:email).and_return(&quot;&quot;)<br> &nbsp; &nbsp;@user.stub!(:password).and_return(&quot;&quot;)<br> &nbsp; &nbsp;@user.stub!(:password_confirmation).and_return(&quot;&quot;)<br><br> &nbsp; &nbsp;#need these to satisfy the error messages<br>
 &nbsp; &nbsp;errors = mock(&quot;errors&quot;)<br> &nbsp; &nbsp;errors.stub!(&quot;empty?&quot;).and_return(false)<br> &nbsp; &nbsp;errors.stub!(&quot;full_messages&quot;).and_return([&quot;error 1&quot;, &quot;error 2&quot;])<br> &nbsp; &nbsp;@account.stub!(:errors).and_return(errors)<br>
 &nbsp; &nbsp;@user.stub!(:errors).and_return(errors)<br><br> &nbsp; &nbsp;Account.stub!(:new).and_return(@account)<br> &nbsp;end<br><br> &nbsp;it &quot;should re-render new template&quot; do<br> &nbsp; &nbsp;post &quot;create&quot; #with no submitted params<br>
 &nbsp; &nbsp;response.should render_template(&quot;accounts/new&quot;)<br> &nbsp;end<br><br># Controller code<br> &nbsp;def create<br> &nbsp; &nbsp;@account = Account.new(params[:account])<br> &nbsp; &nbsp;@user = @account.users.build(params[:user])<br><br> &nbsp; &nbsp;if verify_recaptcha(@account) &amp;&amp; @account.save<br>
 &nbsp; &nbsp; &nbsp;redirect_to admin_listings_url<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;render :action =&gt; &quot;new&quot;<br> &nbsp; &nbsp;end<br> &nbsp;end<br><font color="#888888">--<br>Posted via <a href="http://www.ruby-forum.com/" target="_blank">http://www.ruby-forum.com/</a>.<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>
</font></blockquote></div><br>