amedeiros
amedeiros

Reputation: 949

Testing devise invitable

I am using devise invitable in my app. I have overwritten the devise invitable invitations controller for some custom behavior. When I try and write controller tests for this I get the following error:

NoMethodError: undefined method 'name' for nil:NilClass

That happens when doing get :new.

The route is setup as directed in the setup directions:

devise_for :users, controllers: { sessions: 'users/sessions', invitations: 'users/invitations', registrations: :registrations }

The controller is under /app/controllers/users/invitations_controller.rb and name spaced correctly within the file Users::InvitationsController same with the test /tests/controllers/users/invitations_controller.rb Users::InvitationsControllerTest

This is the test:

context '#new' do
  context 'while not signed in' do
    should 'redirect to the login page' do
      get :new
      assert_redirected_to new_user_session_path
    end
  end
end

This is the action:

def new
  self.resource = resource_class.new
  self.resource.build_group if current_inviter.is_admin?
  self.resource.group = current_inviter.group if current_inviter.is_manager?
  render :new
end

Appreciate any help thanks.

Upvotes: 5

Views: 1731

Answers (1)

Prathamesh Sonpatki
Prathamesh Sonpatki

Reputation: 906

You will need to set Devise.mappings before every request(or in setup block) as follows -

@request.env["devise.mapping"] = Devise.mappings[:user]

This is to help devise map the routes correctly in the tests.

More info can be found here - https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)

Upvotes: 5

Related Questions