0
votes

I'm using devise (3.2.2), cancan (1.6.10) , simple_form (3.0.0). I overrided Devise Registration Controller to allow only the admin to sign_up users, like in Tony tutorial: http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

It works fine with my dev server, but the test fails with rspec.

  **class UsersDeviseRegistrationTest < ActionController::TestCase**
  include Devise::TestHelpers
  tests Users::Devise::RegistrationsController
  fixtures :users
  setup do
    @nuno = users(:nuno)
    @newClient = {
        email: '[email protected]',
        password: '12345678',
        password_confirmation: '12345678'
    }
    request.env['devise.mapping'] = Devise.mappings[:user]
  end
  test "Only admin can create client" do
    sign_in @nuno
    get :new 
    assert :success
    assert_difference('User.count') do
      post :create, user: @newClient
      puts response.body
    end
    assert :success
    assert_redirected_to user_path(assigns(user))
    sign_out @nuno
  end
 end

It fails creating the user... Analysing the response it says:

5 errors prohibited this user from being saved:

Email can't be blank
Email is empty.
Email is invalid
Password can't be blank
Encrypted password can't be blank**

Does anyone have any idea? I already this kind of test in a different demo project, but in that case I was not using cancan. Maybe I am forgetting some config?
Thank you all,

PS: By the way, I don't know if this is important, but in my User model I have:
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable Nuno

1

1 Answers

0
votes

After many headaches, I finally found the solution. Rails4 and strong parameters again. In my custom registrations_controller, I had as strong params:

devise_parameter_sanitizer.for(:sign_up) << [:email]

And this worked just fine in the browser... But in the tests it failed.
So, I replaced by:

params.require(:user).permit(:email, :password, :password_confirmation)

And voilá!
I don't know why it worked in the browser, and any explanation would be great.
Nevertheless, I post the solution for anyone that may come with the same issue, and to improve my karma ;)
Regards