Testing misc methods in ApplicationController

I’m already successfully testing before_filters in
application_controller
courtesy of this blog post:
http://www.movesonrails.com/articles/2008/01/23/spec-ing-your-application-controller

However, i can;'t work out how to test the sort of method that is added
to
application_controller so that all controllers can use it. It must be
something simple that i’m doing wrong/omitting, can anyone help?

eg if i have this empty method in application_controller

def andreplace(str)
end

and my spec looks like this:

describe ApplicationController do
describe “andreplace(str)” do
it “should format ands for ferret search” do
andreplace(“foo and bar”).should eql(“+foo +bar”)
end
end
end

Currently, the test breaks, saying:
NoMethodError in ‘ApplicationController andreplace(str) should format
ands
for ferret search’
undefined method `andreplace’ for
#Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass_1:0xb64f7160

What’s going wrong here, anyone know?

On Fri, Feb 22, 2008 at 7:26 AM, Max W.
[email protected] wrote:

def andreplace(str)
end

and my spec looks like this:

describe ApplicationController do
describe “andreplace(str)” do
it “should format ands for ferret search” do
andreplace(“foo and bar”).should eql(“+foo +bar”)

Try this:

controller.andreplace(“foo and bar”).should eql(“+foo +bar”)

 end

end
end

Currently, the test breaks, saying:
NoMethodError in ‘ApplicationController andreplace(str) should format ands
for ferret search’
undefined method `andreplace’ for
#Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass_1:0xb64f7160

This is a pointer to the problem - the error is telling you that the
#andreplace message is going to the ExampleGroup, not the controller.

HTH,
David

doh…dammit, i thought i’d tried that. I’m losing it…

thanks!

On Fri, Feb 22, 2008 at 7:58 AM, Max W.
[email protected] wrote:

doh…dammit, i thought i’d tried that. I’m losing it…

thanks!

You’re welcome.

Cheers,
David

How do you spec protected controller methods such as before_filters and
helpers that with the params and session hashes?

I cannot call controller.my_method if my_method is protected.

Try this:

controller.andreplace(“foo and bar”).should eql("+foo +bar")

On 13/02/2009, at 7:26 PM, Fernando P. wrote:

How do you spec protected controller methods such as before_filters
and
helpers that with the params and session hashes?

I cannot call controller.my_method if my_method is protected.

Try this:

controller.andreplace(“foo and bar”).should eql("+foo +bar")

Hey Fernando. As Mark said, the idea behind BDD is to spec the
behaviour of your controller, rather than each specific method. So
write specs for all of the different behaviours that your controller
can exhibit. This will result in your protected and private methods
being called.

Cheers,
Nick

On Fri, Feb 13, 2009 at 4:26 PM, Fernando P. [email protected]
wrote:

How do you spec protected controller methods such as before_filters and
helpers that with the params and session hashes?

You spec the public interface that calls (or causes to be called) the
protected methods.

///ark

You spec the public interface that calls (or causes to be called) the
protected methods.

Ok I see. Thanks for the clarification.