【问题标题】:Ruby on Rails functional test with Devise authentication使用 Devise 身份验证的 Ruby on Rails 功能测试
【发布时间】:2011-08-02 12:44:15
【问题描述】:

我正在寻找一个奇怪问题的解决方案。我有一个控制器,需要身份验证(使用设计 gem)。我添加了 Devise TestHelpers,但无法正常工作。

require 'test_helper'

class KeysControllerTest < ActionController::TestCase  
   include Devise::TestHelpers  
   fixtures :keys

   def setup
      @user = User.create!(
        :email => 'testuser@demomailtest.com',
        :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 测试”窗口中得到以下输出:

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>

有人可以告诉我,为什么设计不进行身份验证?我对 AdminController 使用完全相同的程序,并且效果很好。

【问题讨论】:

    标签: ruby-on-rails authentication tdd device functional-testing


    【解决方案1】:

    您是否使用可确认的设计?这种情况下create是不够的,需要用@user.confirm!确认用户

    第二,为什么要在功能测试中创建用户?像这样在夹具中声明您的用户(如果您只需要确认,则为 confirmed_at):

    test/fixtures/users.yml:

    user1: 
    id: 1
    email: user1@test.eu
    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
    

    【讨论】:

    • 太棒了!是的,我正在使用可确认设置。既然像你描述的那样改变它,它就像一个魅力。 :)
    • 这真的有效吗?我的意思是,设计在将密码放入数据库之前对其进行加密,据我所知,夹具直接写入数据库。这意味着解密应该失败(因为 abcdef1 无法正确解密)。
    • 谁能为较新版本的 Devise 添加指向正确答案的指针?
    • 是的,encrypted_pa​​ssword 需要使用内联代码设置才能真正使用加密数据更新列。见stackoverflow.com/a/10427785/18706
    【解决方案2】:

    我花了一些时间才弄明白,但事实证明答案很简单。

    重点是,在您的设备文件 (users.yml) 中,您需要确保用户已“确认”(假设您在 User 模型中指定了“可确认”)。所以,例如,把它放在你的 users.yml 中:

    用户一个: 确认时间:2015/01/01

    就是这样,无需指定其他字段(电子邮件、加密密码等)。

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

    登录用户(:user_one)

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

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题(但使用的是 FactoryGirl,而不是 Fixtures)并且能够通过简单地使用 FactoryGirl.create(:user) 而不是 FactoryGirl.build(:user) 来解决它。

      显然,Devise 要求用户已被持久化到数据库中,以便一切正常工作。

      【讨论】:

        猜你喜欢
        • 2010-10-26
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-28
        • 2018-04-27
        相关资源
        最近更新 更多