# Stubbing an ApplicationController before\_filter

**URL:** <https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708>\
**Category:** Rails\
**Created:** [April 16, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708 "2009-04-16T16:39:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 16, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/1 "2009-04-16T16:39:50Z")

</div>

Hi,

I am writing a functional test for a controller. The problem I have is  
that a before filter defined in ApplicationController is hit and I don’t  
know how to stub it.

I tried controller.expects(:find\_site).returns(@site) but it fails as  
controller is unknown method.

I tried ApplicationController.expects(:find\_site).returns(@site) but the  
assertion fails as it never happens. So how do I deal with that?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 16, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/2 "2009-04-16T17:43:54Z")

</div>

Hi,

The RSpec Book ([Pragmatic Bookshelf: By Developers, For Developers](http://www.pragprog.com/titles/achbd/the-rspec-book))  
just added a new chapter about testing controllers, within which is an  
example of testing a before filter. You might find it of use.

Brandon

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 16, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/3 "2009-04-16T16:58:44Z")

</div>

I tried the following:

def setup  
@controller = ProductsController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new

```
@site = Site.new(:domain_name => 'example.com')
@controller.expects(:find_site).returns(@site)

```

end

But with the debugger, once inside the action of the controller being  
tested, @site is not set, and therefore the action cannot run correctly.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 17, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/4 "2009-04-17T15:13:02Z")

</div>

Fernando P. wrote:  
[…]

> I stopped using RSpec,

Just for this issue? If so, there was no need. I use  
controller.stub!(:filter).return(@whatever)  
regularly with RSpec. It works fine.

> and switched back to Test::Unit, the Agile Web  
> dev with Rails has a good intro chapter and I was able to solve my  
> problem.

How were you able to solve the problem? It will help others if you post  
your solution.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 17, 2009, 11:32am UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/5 "2009-04-17T11:32:23Z")

</div>

Brandon O. wrote:

> Hi,
> 
> The RSpec Book ([Pragmatic Bookshelf: By Developers, For Developers](http://www.pragprog.com/titles/achbd/the-rspec-book))  
> just added a new chapter about testing controllers, within which is an  
> example of testing a before filter. You might find it of use.
> 
> Brandon

I stopped using RSpec, and switched back to Test::Unit, the Agile Web  
dev with Rails has a good intro chapter and I was able to solve my  
problem.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 17, 2009, 3:29pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/6 "2009-04-17T15:29:52Z")

</div>

> Just for this issue? If so, there was no need.  
> Not just for that issue, and Brandon probably assumed I was using rspec,  
> but it pissed me off some time ago.

Each time I would hit a wall with RSpec and have to search for hours how  
to solve it. With Test::Unit for some reason, I don’t have as many  
little annoying troubles, and when I do, I find a solution whether on my  
own or by reading stuff online very quickly. I am having a much better  
testing experience with Test::Unit.

> How were you able to solve the problem? It will help others if you post  
> your solution.

Well I can’t directly stub a controller instance method. So I stubbed  
the Model class method that the instance variable will be set to.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 17, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/7 "2009-04-17T17:26:16Z")

</div>

> Only because I could have sworn I saw you posting over on that mailing  
> list just the other day.  
> I didn’t fully transition to test::unit yet, so I still use RSpec’s ML  
> from time to time.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 17, 2009, 4:50pm UTC](https://www.ruby-forum.com/t/stubbing-an-applicationcontroller-before-filter/165708/8 "2009-04-17T16:50:27Z")

</div>

> Not just for that issue, and Brandon probably assumed I was using  
> rspec,

Only because I could have sworn I saw you posting over on that mailing  
list just the other day.

Brandon
