Forum: Ruby on Rails Stubbing an ApplicationController before_filter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fernando P. (Guest)
on 2009-04-16 18:39
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?
Fernando P. (Guest)
on 2009-04-16 18:58
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.
Brandon O. (Guest)
on 2009-04-16 19:43
(Received via mailing list)
Hi,

The RSpec Book (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
Fernando P. (Guest)
on 2009-04-17 13:32
Brandon O. wrote:
> Hi,
>
> The RSpec Book (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.
Marnen L. (Guest)
on 2009-04-17 17:13
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
removed_email_address@domain.invalid
Fernando P. (Guest)
on 2009-04-17 17:29
> 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.
Brandon O. (Guest)
on 2009-04-17 18:50
(Received via mailing list)
> 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
Fernando P. (Guest)
on 2009-04-17 19:26
> 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.
This topic is locked and can not be replied to.