微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 使用Devise身份验证的Ruby on Rails功能测试

我正在寻找一个奇怪的问题的解决方案.我有一个控制器,需要认证(与设计宝石).我添加了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

结束

我在“耙式测试”窗口中得到以下输出

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,它的工作完美.

解决方法

您是否使用Devise确认?在这种情况下,创建还不够,您需要使用@ user.confirm确认用户

第二,为什么在功能测试中创建用户在这样的灯具中声明你的用户(如果你只需要确认,confirm_at):

测试/装置/ 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

原文地址:https://www.jb51.cc/ruby/266586.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐