[rspec-users] Could this controller test be made simpler?
Andrew WC Brown
omen.king at gmail.com
Mon Jan 28 14:45:15 EST 2008
Been a month since I've rspec, I think you can place the stubs here.
@user = mock_model(User,
login => "",
email => "",
password => "",
password_confirmation => ""
@account = mock_model(Account,
subdomain => "",
company_name => "",
filename => ""
On Jan 28, 2008 2:36 PM, Chris Olsen <lists at ruby-forum.com> wrote:
> In a create controller method I am testing the else clause that occurs
> when the model is unable to be saved. Sounds simple enough, but because
> model is created before the save and the form has to be re-filled I now
> have to stub a model and all the attributes. Since the error messages
> are displayed I also have to stub some methods within the model that
> allow the error messages to be displayed properly.
> This small test turns into a lot of code. So I am wondering if there is
> something that I am missing to make this much simpler. Things did start
> simple, in the stubbing of the new method within the Account class and
> then the save method within the @account model. Then I get errors
> reminding me to stub out the users method for the account model, then
> the build method, etc
> I have looked at the generated scaffold code and it is simpler, but it
> seems that the relationship made between the two models is what starts
> to make this more complex.
> Thanks for the help.
> Here is my current code:
> before :each do
> @account = mock_model(Account)
> #what is being tested here
> #User info
> @user = mock_model(User)
> users = mock("Userlist")
> #since the form is repopulated the account mock must have stub for
> all the attributes
> #need these to satisfy the error messages
> errors = mock("errors")
> errors.stub!("full_messages").and_return(["error 1", "error 2"])
> it "should re-render new template" do
> post "create" #with no submitted params
> response.should render_template("accounts/new")
> # Controller code
> def create
> @account = Account.new(params[:account])
> @user = @account.users.build(params[:user])
> if verify_recaptcha(@account) && @account.save
> redirect_to admin_listings_url
> render :action => "new"
> Posted via http://www.ruby-forum.com/.
> rspec-users mailing list
> rspec-users at rubyforge.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the rspec-users