# Protected ApplicationController methods not called in Tests?

**URL:** <https://www.ruby-forum.com/t/protected-applicationcontroller-methods-not-called-in-tests/192883>\
**Category:** Rails\
**Created:** [August 7, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/protected-applicationcontroller-methods-not-called-in-tests/192883 "2010-08-07T17:05:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [August 7, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/protected-applicationcontroller-methods-not-called-in-tests/192883/1 "2010-08-07T17:05:28Z")

</div>

Hey,

I’m currently writing some tests and getting a strange error. My  
ApplicationController has following protected method:  
before\_filter :load\_user\_vars

def load\_user\_vars  
@current\_user = current\_user [other protected method]  
@current\_character = @current\_user.character  
…  
end

When I now run a test and the @current\_character is being used in a  
controller that is being tested I get a “RuntimeError: Called id for  
nil”

Why is that protected method not being called since it’s a before\_filter  
method? And how can I make my test to run this method and declare these  
variables?

Thanks!

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [August 7, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/protected-applicationcontroller-methods-not-called-in-tests/192883/2 "2010-08-07T17:26:17Z")

</div>

On 7 August 2010 16:05, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> When I now run a test and the @current\_character is being used in a  
> controller that is being tested I get a “RuntimeError: Called id for  
> nil”
> 
> Why is that protected method not being called since it’s a before\_filter  
> method? And how can I make my test to run this method and declare these  
> variables?

It certainly should be called from tests. Are you sure it is not being  
called?  
Try putting a line  
debugger  
at the start of the filter and see if it breaks there.

Otherwise how are you invoking the controller method that should call  
the filter?

Have you looked in test.log to see if there is anything of note there?

If still no joy show us the controller action code and the error trace  
and which line it is failing on.

Colin

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [August 7, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/protected-applicationcontroller-methods-not-called-in-tests/192883/3 "2010-08-07T17:32:44Z")

</div>

On 7 August 2010 16:05, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I now run a test and the @current\_character is being used in a  
> controller that is being tested I get a “RuntimeError: Called id for  
> nil”
> 
> Why is that protected method not being called since it’s a before\_filter  
> method? And how can I make my test to run this method and declare these  
> variables?

How do you conclude that load\_user\_vars isn’t being run? Perhaps it is  
being run, but @current\_user.character is just returning nil (which  
seems more likely). It’s hard to be sure without seeing some more  
code. Could you Gist [1] your ApplicationController, and the code  
where the RuntimeError is being raised?

Chris

[1] [http://gist.github.com/](http://gist.github.com/)

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [August 7, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/protected-applicationcontroller-methods-not-called-in-tests/192883/4 "2010-08-07T18:00:09Z")

</div>

Sorry guys, It took me only like ten times going through the code to  
find a typo that caused the problem. It’s working now, thanks for the  
quick help!
