我正在使用设计(3.2.2),cancan(1.6.10),simple_form(3.0.0) . 我重写了Devise Registration Controller,只允许admin给sign_up用户,比如Tony教程:http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/
它与我的开发服务器一起工作正常,但是测试失败了 .
**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 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**
有谁有想法吗?我已经在不同的演示项目中进行了这种测试,但在那种情况下我没有使用cancan . 也许我忘了一些配置?
谢谢你们,
PS:顺便说一句,我不知道这是否重要,但在我的用户模型中,我有:
设计:database_authenticatable,:recoverable,:rememberable,:trackable,:validatable,:registerable Nuno
1 回答
经过多次头痛,我终于找到了解决方案 . Rails4和强参数再次出现 . 在我的自定义registrations_controller中,我有一个强大的参数:
这在浏览器中工作得很好......但是在测试中它失败了 .
所以,我换成:
瞧!
我不知道为什么它在浏览器中有效,任何解释都会很棒 .
尽管如此,我为可能带有同样问题的任何人发布解决方案,并提高我的业力;)
问候