stffn / declarative_authorization

An unmaintained authorization plugin for Rails. Please fork to support current versions of Rails
MIT License
1.24k stars 231 forks source link

Controller test with rspec/capybara fails with: @controller is nil #178

Open nagyt234 opened 11 years ago

nagyt234 commented 11 years ago

I am using rspec/capybara for testing. Model testing goes well with with_user, but I can not run controller test with get_with. The following simple test gives the error message:

Failures:

  1) Revision pages Index Authorisations Authorisation error 
     Failure/Error: get_with @admin1, revisions_path
     RuntimeError:
       @controller is nil: make sure you set it in your test's setup method.
     # ./spec/controllers/revisions_controller_spec.rb:23:in `block (5 levels) in <top (required)>'

The test script:

require 'spec_helper'
require 'shared/user_helper'

describe "Revision pages" do

  include SpecUserHelpers

  subject { page }

  describe "Index" do

    describe "Authorisations" do
      let(:auth_error_text) { "Sorry, you are not allowed to access that page." }

      before do
        without_access_control do
          create_users_with_roles               # see shared/user_helper
        end
      end

      describe "Authorisation error" do
        before do
          get_with @admin1, revisions_path
        end
        it { should have_selector('div.alert.alert-error', text: auth_error_text) }
      end
    end
  end
end

Do I something wrong? In the place of get_with the standard capybara visit revisions_path works, but I must set the user to test authorizations. I tried also

  before do
    with_user @admin1
      visit revisions_path
    end
  end

but then I se in the log that the Anonymous user is used. But with_user is for model testing.