Stubbing an ApplicationController before_filter

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?

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 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.

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
[email protected]

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.

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.

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs