使用 Devise 身份验证进行 Ruby on Rails 功能测试

发布于 2024-10-31 04:47:50 字数 2032 浏览 1 评论 0原文

我正在寻找一个奇怪问题的解决方案。我有一个控制器,需要身份验证(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2024-11-07 04:47:50

您是否正在使用带有可确认功能的 Devise?这种情况下,create是不够的,需要用@user.confirm!来确认用户。

其次,功能测试中为什么要创建用户呢?在固定装置中声明您的用户,如下所示(如果您只需要确认,则为confirmed_at):

test/fixtures/users.yml:

user1: 
id: 1
email: [email protected]
encrypted_password: abcdef1
password_salt:  efvfvffdv
confirmed_at: <%= Time.now %>

并使用以下命令在功能测试中登录它们:

sign_in users(:user1)

编辑:我刚刚看到,在我的应用程序中,Devise-Testhelpers 是在 test/test-helpers.rb 中声明,我不知道这是否有什么不同,也许你想尝试:

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActionController::TestCase
  include Devise::TestHelpers
end

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

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:

user1: 
id: 1
email: [email protected]
encrypted_password: abcdef1
password_salt:  efvfvffdv
confirmed_at: <%= Time.now %>

and sign them in in your functional tests with:

sign_in users(:user1)

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:

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActionController::TestCase
  include Devise::TestHelpers
end

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
end
电影里的梦 2024-11-07 04:47:50

我花了一些时间才弄清楚,但事实证明答案非常简单。

要点是,在您的装置文件(users.yml)中,您需要确保用户已“确认”(假设您在用户模型中指定了“可确认”)。因此,例如,将其放入您的 users.yml 中:

user_one:
  confirmed_at: 2015/01/01

仅此而已,无需指定其他字段(电子邮件、加密密码等)。

现在,在您的控制器测试中(例如在“设置”或“之前”中),您只需编写代码:

sign_in users(:user_one)

然后它就应该可以工作了!

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:

user_one:
  confirmed_at: 2015/01/01

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:

sign_in users(:user_one)

And then it should just work!

悸初 2024-11-07 04:47:50

我遇到了类似的问题(但使用 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 than FactoryGirl.build(:user).

Evidently, Devise requires the user to have been persisted to the Database, for everything to work properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文