Failure trying to test ApplicationController

I’m trying to write some tests for the ApplicationController as shared
tests that can be run in all of my other controller tests, but am
getting
a nil.rewrite error. Below is what I have…

describe AccountController do
it_should_behave_like ‘Application controller’
end

describe ‘Application controller’, :shared => true do

it ‘should fail require_login check if not logged in’ do
User.current = nil
controller.require_login.should be_false
response.should redirect_to(:controller => ‘account’, :action =>
‘logon’)
end
end

def require_login
return true if User.current.logged_in?

redirect_to :controller => “account”, :action => “logon” and return
false
end

The line it’s failing on is the redirect_to in require_login. What am I
doing wrong?

Thanks,
Steve

On 10/15/07, Steve [email protected] wrote:

it ‘should fail require_login check if not logged in’ do
end

The line it’s failing on is the redirect_to in require_login. What am I
doing wrong?

What’s the error?

On Mon, 15 Oct 2007 23:46:32 -0500, David C. wrote:

redirect_to :controller => “account”, :action => “logon” and return false
end

The line it’s failing on is the redirect_to in require_login. What am I
doing wrong?

What’s the error?

NoMethodError in ‘AccountController should fail require_login check if
not logged in’
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.rewrite
trunk/app/controllers/application.rb:28:in `require_login’
./spec/controllers/application_controller_spec.rb:12:

The “redirect_to” call is line 28, and “controller.require_login.should”
is line 12.

On Tue, 16 Oct 2007 00:26:13 -0500, David C. wrote:

To be honest, I’m not quite sure how to spec this. One way would be to
set a message expectation for the call to redirect_to:

controller.should_receive(:redirect_to)
controller.require_login

Anybody else have any ideas?

I see what you’re saying I think. That the application controller stuff
isn’t setup properly because no initial request(get/post) has been
made.

Attaching these tests to every describe out there for a given
controller seems overkill. Since if the one spec for testing the
application controller tests all of its methods, then it should be fine
wherever its used it would seem. Would it be enough to just make a call
to
some controller/action in the app to get everything initialized?

On 10/15/07, Steve [email protected] wrote:

it ‘should fail require_login check if not logged in’ do
User.current = nil
controller.require_login.should be_false
response.should redirect_to(:controller => ‘account’, :action => ‘logon’)

You get responses (i.e. the response object in the spec) from actions
using get, post, put, delete, not by calling methods directly on the
controller.

Part of the problem is that you’re trying to spec something that
already exists. Developing spec-first, you wouldn’t likely end up with
this problem because this method would have appeared because you
refactored it out of an existing action, which was already spec’d.

To be honest, I’m not quite sure how to spec this. One way would be to
set a message expectation for the call to redirect_to:

controller.should_receive(:redirect_to)
controller.require_login

Anybody else have any ideas?

On Tue, 16 Oct 2007 01:02:58 -0500, David C. wrote:

But how do you know that it’s being used? The behaviour you’re
spec’ing is that a given action should redirect if the user is not
logged in, right? If you look at it that way, then it seems perfectly
acceptable to do this:

describe “some action on some controller” do
it_should_behave_like “action that requires login”

end

I see what you’re saying now, and agree that that’s probably the best
way
to approach it.

Thanks.

On 10/16/07, Steve [email protected] wrote:

Attaching these tests to every describe out there for a given
controller seems overkill. Since if the one spec for testing the
application controller tests all of its methods, then it should be fine
wherever its used it would seem.

But how do you know that it’s being used? The behaviour you’re
spec’ing is that a given action should redirect if the user is not
logged in, right? If you look at it that way, then it seems perfectly
acceptable to do this:

describe “some action on some controller” do
it_should_behave_like “action that requires login”

end

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