# Need some help with a simple

**URL:** <https://www.ruby-forum.com/t/need-some-help-with-a-simple/134113>\
**Category:** RSpec\
**Created:** [April 7, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/need-some-help-with-a-simple/134113 "2008-04-07T23:14:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumwutdum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@sumwutdum](https://www.ruby-forum.com/u/sumwutdum)\
**Post date:** [April 7, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/need-some-help-with-a-simple/134113/1 "2008-04-07T23:14:06Z")

</div>

I can’t figure out why this is failing. It seems to have a hard time  
assigning the @account. I have spent way too long trying to figure this  
out.

Thanks for any help.

* * *

## NoMethodError in ‘StaffMembersController GET index should allow anonymous users’ You have a nil object when you didn’t expect it! The error occurred while evaluating nil.staff\_members

++++++++++++++++  
describe StaffMembersController do

describe “GET index” do

```
before :each do
  @account = mock_model(Account, :staff_members =>

```

[mock\_model(StaffMember)])  
controller.stub!(:find\_account\_by\_subdomain\_or\_url).and\_return(@account)  
end

```
# Authorization
it "should allow anonymous users" do
  get :index
  response.should render_template(:index)
end

```

end  
end  
++++++++++++++++

StaffMembersController  
++++++++++++++++  
class StaffMembersController \< ApplicationController  
before\_filter :find\_account\_by\_subdomain\_or\_url

def index  
@staff\_members = @account.staff\_members  
end

end  
+++++++++++++++

---

<div class="post-metadata">

**Author:** ![sumwutdum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@sumwutdum](https://www.ruby-forum.com/u/sumwutdum)\
**Post date:** [April 7, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/need-some-help-with-a-simple/134113/2 "2008-04-07T23:33:45Z")

</div>

I’m guessing the key is inside “find\_account\_by\_subdomain\_or\_url”. Do  
you have that defined somewhere? It probably calls something like:

@account = Account.find\_by\_subdomain(blah) or Account.find\_by\_url(blah)

In which case that’s probably what you need to stub. Nothing is  
actually checking for the return value of the controller method or  
assigning it to @account. In other words, you probably should do  
Account.stub! instead of controller.stub! Check what’s in that method  
and see if that gets you closer to the solution.

Good luck!  
Glenn

---

<div class="post-metadata">

**Author:** ![sumwutdum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@sumwutdum](https://www.ruby-forum.com/u/sumwutdum)\
**Post date:** [April 7, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/need-some-help-with-a-simple/134113/3 "2008-04-07T23:54:48Z")

</div>

Glenn F. wrote:

> I’m guessing the key is inside “find\_account\_by\_subdomain\_or\_url”. Do  
> you have that defined somewhere? It probably calls something like:
> 
> @account = Account.find\_by\_subdomain(blah) or Account.find\_by\_url(blah)
> 
> In which case that’s probably what you need to stub. Nothing is  
> actually checking for the return value of the controller method or  
> assigning it to @account. In other words, you probably should do  
> Account.stub! instead of controller.stub! Check what’s in that method  
> and see if that gets you closer to the solution.
> 
> Good luck!  
> Glenn

That worked.

I was under the impression that there would be no subdomain available  
when testing, which is why I was stubbing the controller method. After  
it working I actually checked what the request.domain was and saw that  
it is test.host 🙂

Thanks Glenn
