繁体   English   中英

Devise / Cancan控制器测试注册失败-RSpec

[英]Devise/Cancan controller test sign_up failing - rspec

我正在使用devise(3.2.2),cancan(1.6.10)和simple_form(3.0.0)。 我重写了Devise Registration Controller,使其仅允许管理员注册用户,如Tony教程中所示: http : //www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise -控制器/

它可以在我的开发服务器上正常工作,但是在rspec下测试失败。

  **class UsersDeviseRegistrationTest < ActionController::TestCase**
  include Devise::TestHelpers
  tests Users::Devise::RegistrationsController
  fixtures :users
  setup do
    @nuno = users(:nuno)
    @newClient = {
        email: 'cliNew@cliOne.com',
        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

创建用户失败。...分析响应并显示:

禁止保存此用户的5个错误:

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

有人有什么主意吗? 我已经在另一个演示项目中进行了这种测试,但是在那种情况下,我没有使用cancan。 也许我忘记了一些配置?
谢谢你们,

PS:顺便说一句,我不知道这是否重要,但是在我的用户模型中,我有:
设计:database_authenticatable,:recoverable,:rememberable,:trackable,:validatable,:registerable Nuno

经过许多头痛之后,我终于找到了解决方案。 Rails4和强参数再次出现。 在我的自定义registrations_controller中,我有一些很强的参数:

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

而且这在浏览器中工作得很好……但是在测试中却失败了。
因此,我替换为:

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

瞧!
我不知道为什么它可以在浏览器中工作,任何解释都很好。
不过,我将解决方案发布给可能遇到相同问题的任何人,以提高我的业力;)
问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM