使用 Devise 身份验证进行 Ruby on Rails 功能测试
我正在寻找一个奇怪问题的解决方案。我有一个控制器,需要身份验证(使用 devise gem)。我添加了 Devise TestHelpers 但无法让它工作。
require 'test_helper'
class KeysControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :keys
def setup
@user = User.create!(
:email => '[email protected]',
:password => 'MyTestingPassword',
:password_confirmation => 'MyTestingPassword'
)
sign_in @user
@key = keys(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:keys)
end
test "should get new" do
get :new
assert_response :success
end
test "should create key" do
assert_difference('Key.count') do
post :create, :key => @key.attributes
end
assert_redirected_to key_path(assigns(:key))
end
test "should destroy key" do
assert_difference('Key.count', -1) do
delete :destroy, :id => @key.to_param
end
assert_redirected_to keys_path
end
我在“rake test”窗口
中得到以下输出:
29) Failure:
test_should_create_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:29]:
"Key.count" didn't change by 1.
<3> expected but was
<2>.
30) Failure:
test_should_destroy_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:37]:
"Key.count" didn't change by -1.
<1> expected but was
<2>.
31) Failure:
test_should_get_index(KeysControllerTest) [/test/functional/keys_controller_test.rb:19]:
Expected response to be a <:success>, but was <302>
32) Failure:
test_should_get_new(KeysControllerTest) [/test/functional/keys_controller_test.rb:25]:
Expected response to be a <:success>, but was <302>
有人可以告诉我,为什么 devise 无法进行身份验证吗?我对 AdminController 使用完全相同的过程,并且效果完美。
I'm searching for a solution for a weird problem. I have a controller, that needs authentication (with the devise gem). I added the Devise TestHelpers but i can't get it working.
require 'test_helper'
class KeysControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :keys
def setup
@user = User.create!(
:email => '[email protected]',
:password => 'MyTestingPassword',
:password_confirmation => 'MyTestingPassword'
)
sign_in @user
@key = keys(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:keys)
end
test "should get new" do
get :new
assert_response :success
end
test "should create key" do
assert_difference('Key.count') do
post :create, :key => @key.attributes
end
assert_redirected_to key_path(assigns(:key))
end
test "should destroy key" do
assert_difference('Key.count', -1) do
delete :destroy, :id => @key.to_param
end
assert_redirected_to keys_path
end
end
And i get the following output in my "rake test" window:
29) Failure:
test_should_create_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:29]:
"Key.count" didn't change by 1.
<3> expected but was
<2>.
30) Failure:
test_should_destroy_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:37]:
"Key.count" didn't change by -1.
<1> expected but was
<2>.
31) Failure:
test_should_get_index(KeysControllerTest) [/test/functional/keys_controller_test.rb:19]:
Expected response to be a <:success>, but was <302>
32) Failure:
test_should_get_new(KeysControllerTest) [/test/functional/keys_controller_test.rb:25]:
Expected response to be a <:success>, but was <302>
Can someone tell my, why devise doesn't authenticate? I'm using the exact same procedure for an AdminController and it works perfect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否正在使用带有可确认功能的 Devise?这种情况下,create是不够的,需要用
@user.confirm!
来确认用户。其次,功能测试中为什么要创建用户呢?在固定装置中声明您的用户,如下所示(如果您只需要确认,则为confirmed_at):
test/fixtures/users.yml:
并使用以下命令在功能测试中登录它们:
编辑:我刚刚看到,在我的应用程序中,Devise-Testhelpers 是在 test/test-helpers.rb 中声明,我不知道这是否有什么不同,也许你想尝试:
Are you using Devise with confirmable? In this case, create is not enough and you need to confirm the user with
@user.confirm!
Second, why do you create the user in the functional test? Declare your users in the fixture like this (confirmed_at if you require confirmation only):
test/fixtures/users.yml:
and sign them in in your functional tests with:
Edit: I just saw, that in my app the Devise-Testhelpers are declared in test/test-helpers.rb and I don't know if this makes a difference, maybe you want to try:
我花了一些时间才弄清楚,但事实证明答案非常简单。
要点是,在您的装置文件(users.yml)中,您需要确保用户已“确认”(假设您在用户模型中指定了“可确认”)。因此,例如,将其放入您的 users.yml 中:
仅此而已,无需指定其他字段(电子邮件、加密密码等)。
现在,在您的控制器测试中(例如在“设置”或“之前”中),您只需编写代码:
然后它就应该可以工作了!
This took me some time to figure out but it turns out the answer is really simple.
The point is that, in your fixtures file (users.yml), you need to make sure the user is 'confirmed' (assuming that you specified "confirmable" in your User model). So, for instance, put this in your users.yml:
That's all, no need to specify other fields (email, encrypted password, etc).
Now in your controller test (e.g. in 'setup' or in 'before') you simply code:
And then it should just work!
我遇到了类似的问题(但使用 FactoryGirl,而不是 Fixtures),并且只需使用
FactoryGirl.create(:user)
而不是FactoryGirl.build(:user)< /代码>。
显然,Devise 要求用户已保存到数据库,以便一切正常工作。
I had a similar issue (but using FactoryGirl, rather than Fixtures) and was able to resolve it by simply using
FactoryGirl.create(:user)
rather thanFactoryGirl.build(:user)
.Evidently, Devise requires the user to have been persisted to the Database, for everything to work properly.