# Controller spec gives strange error

**URL:** <https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078>\
**Category:** RSpec\
**Created:** [July 3, 2008, 9:24am UTC](https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078 "2008-07-03T09:24:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [July 3, 2008, 9:24am UTC](https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078/1 "2008-07-03T09:24:15Z")

</div>

Good day!

I have a Rails app with a controller that sets an instance variable in a  
before\_filter, like this:

class PostsController \< ApplicationController

before\_filter :set\_site\_language

def set\_site\_language  
@site\_language = cookie[‘lang’] ?  
Language.find\_by\_code(cookie[‘lang’]) :  
Language.find(:first)  
end

def index  
@posts = Post.find\_by\_language\_id(@site\_language.id)  
end

end

The controller has the following spec:

describe PostsController do

it “should show home page” do  
get :index  
response.should be\_success  
end

end

And the equivalent test:

class PostsControllerTest \< ActionController::TestCase

def test\_should\_show\_home\_page  
get :index  
assert\_response :success  
end

end

The test runs fine, but the spec fails with the following error on line  
with `@posts = Post.find_by_language_id(@site_language.id)`:

RuntimeError in ‘PostsController should show home page’  
Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

Seems that @site\_language does not get assigned when spec runs. Could  
you please help me solve this problem?

Yours,  
Damian

---

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [July 3, 2008, 9:45am UTC](https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078/2 "2008-07-03T09:45:48Z")

</div>

On Jul 3, 2008, at 2:24 AM, Damian T. wrote:

> def set\_site\_language  
> @site\_language = cookie[‘lang’] ?  
> Language.find\_by\_code(cookie[‘lang’]) :  
> Language.find(:first)

Shouldn’t this be cookies, not cookie? Doesn’t explain why the test  
passes when the example fails, but it seems that both should be  
raising errors on this.

---

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [July 3, 2008, 9:51am UTC](https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078/3 "2008-07-03T09:51:32Z")

</div>

David C. wrote:

> On Jul 3, 2008, at 2:24 AM, Damian T. wrote:
> 
> > def set\_site\_language  
> > @site\_language = cookie[‘lang’] ?  
> > Language.find\_by\_code(cookie[‘lang’]) :  
> > Language.find(:first)
> 
> Shouldn’t this be cookies, not cookie? Doesn’t explain why the test  
> passes when the example fails, but it seems that both should be  
> raising errors on this.

Yes, there should be `cookies`, but the problem still exists.

---

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [July 3, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078/4 "2008-07-03T18:46:18Z")

</div>

I believe the problem is that you’re not telling the Language model  
what to stub/mock when it calls the find method inside of  
set\_site\_language.

I’m not entirely sure, but I would imagine that the TestUnit test is  
passing, because it’s using the full Rails stack to run the test.

Try doing something like this (code below):

On Jul 3, 2008, at 2:24 AM, Damian T. wrote:

> def set\_site\_language
> 
> The controller has the following spec:
> 
> describe PostsController do

before(:each) do  
@language\_mock = mock\_model(Language)

controller.stub!(:set\_site\_language).and\_return @language\_mock  
end

---

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [July 4, 2008, 8:08am UTC](https://www.ruby-forum.com/t/controller-spec-gives-strange-error/142078/5 "2008-07-04T08:08:32Z")

</div>

Bryan R. wrote:

> I believe the problem is that you’re not telling the Language model  
> what to stub/mock when it calls the find method inside of  
> set\_site\_language.
> 
> I’m not entirely sure, but I would imagine that the TestUnit test is  
> passing, because it’s using the full Rails stack to run the test.
> 
> Try doing something like this (code below):

Thanks for your help!  
Anyway, switching to Test::Spec has solved the problem.
