<div>Been a month since I'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 => "",</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>email => "",</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>password<span class="Apple-tab-span" style="white-space:pre">        </span>=> "",</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>password_confirmation => ""</div><div>)</div><div><br class="webkit-block-placeholder"></div><div>@account = mock_model(Account, </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>subdomain => "",</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>company_name => "",</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>filename => ""</div><div>)</div><br><div class="gmail_quote">
On Jan 28, 2008 2:36 PM, Chris Olsen <<a href="mailto:lists@ruby-forum.com">lists@ruby-forum.com</a>> 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. 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. 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. So I am wondering if there is<br>
something that I am missing to make this much simpler. 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. 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> before :each do<br> @account = mock_model(Account)<br><br> #what is being tested here<br> @account.stub!(:save).and_return(false)<br><br> #User info<br> @user = mock_model(User)<br>
users = mock("Userlist")<br> @account.stub!(:users).and_return(users)<br> users.stub!(:build).and_return(@user)<br><br> #since the form is repopulated the account mock must have stub for<br>all the attributes<br>
@account.stub!(:subdomain).and_return("")<br> @account.stub!(:company_name).and_return("")<br> @account.stub!(:filename).and_return("")<br> @user.stub!(:login).and_return("")<br>
@user.stub!(:email).and_return("")<br> @user.stub!(:password).and_return("")<br> @user.stub!(:password_confirmation).and_return("")<br><br> #need these to satisfy the error messages<br>
errors = mock("errors")<br> errors.stub!("empty?").and_return(false)<br> errors.stub!("full_messages").and_return(["error 1", "error 2"])<br> @account.stub!(:errors).and_return(errors)<br>
@user.stub!(:errors).and_return(errors)<br><br> Account.stub!(:new).and_return(@account)<br> end<br><br> it "should re-render new template" do<br> post "create" #with no submitted params<br>
response.should render_template("accounts/new")<br> end<br><br># Controller code<br> def create<br> @account = Account.new(params[:account])<br> @user = @account.users.build(params[:user])<br><br> if verify_recaptcha(@account) && @account.save<br>
redirect_to admin_listings_url<br> else<br> render :action => "new"<br> end<br> 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>