# Trouble Testing a Controller With RSpec

**URL:** <https://www.ruby-forum.com/t/trouble-testing-a-controller-with-rspec/146092>\
**Category:** Rails\
**Created:** [August 17, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/trouble-testing-a-controller-with-rspec/146092 "2008-08-17T19:57:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [August 17, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/trouble-testing-a-controller-with-rspec/146092/1 "2008-08-17T19:57:02Z")

</div>

Hello-

I’ve been staring that this problem for hours with no success. What  
I’m trying to do is pretty simple. I’m trying to test the create  
method of a controller.

Here is the create method:

# POST /accounts

def create  
@account = Account.new(params[:account])

```
respond_to do |format|
  if @account.save
    flash[:notice] = 'Account was successfully created.'
    format.html { redirect_to account_url }
  else
    format.html { render :action => "new" }
  end
end

```

end

My Account class only has two fields: name and subdomain.

Here is the code for my test:

```
before(:each) do
  @account = mock_model(Account, :to_param => "1")
  Account.stub!(:new).and_return(@account)
end

def do_post
  @account.should_receive(:save).and_return(true)
  post :create, :account => { :subdomain => 'test' }
end

```

My test is failing because of the line in my controller “format.html  
{ redirect\_to account\_url }”. I’m using the account location plugin  
and when the account\_url method is called, it is looking for  
@account.subdomain, which is not set. In fact, any parameters I pass  
in to the call to “post” in my test seem to be ignored.

Am I doing something wrong? Thanks in advance.

-Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [August 17, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/trouble-testing-a-controller-with-rspec/146092/2 "2008-08-17T22:21:46Z")

</div>

On Sun, Aug 17, 2008 at 1:56 PM, emarthinsen  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> before(:each) do  
> @account = mock\_model(Account, :to\_param =\> “1”)  
> Account.stub!(:new).and\_return(@account)

```
    controller.stub!(account_url).and_return("redirect_value")

```

> @account.subdomain, which is not set.  
> So stub it out, see above.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [August 17, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/trouble-testing-a-controller-with-rspec/146092/3 "2008-08-17T22:29:23Z")

</div>

Hi Rick. Thanks for the reply. Is stubbing that out a hack or the best  
way to appraoch this. I’m worried that by stubbing out the account\_url  
method, I won’t be testing whether or not the account\_url method is  
properly accessing @account or that @acount is being preoperly set  
from the params. Granted, a case could be made that those are separate  
tests or are unnecessary (given that testing params is more testing  
Rails than my code). What are our thoughts? Thanks again.

-Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [August 17, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/trouble-testing-a-controller-with-rspec/146092/4 "2008-08-17T23:13:20Z")

</div>

On Sun, Aug 17, 2008 at 4:28 PM, emarthinsen  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Hi Rick. Thanks for the reply. Is stubbing that out a hack or the best  
> way to appraoch this. I’m worried that by stubbing out the account\_url  
> method, I won’t be testing whether or not the account\_url method is  
> properly accessing @account or that @acount is being preoperly set  
> from the params. Granted, a case could be made that those are separate  
> tests or are unnecessary (given that testing params is more testing  
> Rails than my code). What are our thoughts? Thanks again.

I’d consider the account\_url helper to be part of rails, and in any case  
outside of the scope of this controller’s spec.

In your original post you mentioned a plugin which seems to be affecting  
account\_url, in which case I’d expect this to be specified in the  
tests/specs for that plugin.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
