Ruby on rails rubyonrails功能测试与designe认证

Ruby on rails rubyonrails功能测试与designe认证,ruby-on-rails,authentication,tdd,device,functional-testing,Ruby On Rails,Authentication,Tdd,Device,Functional Testing,我在为一个奇怪的问题寻找解决方案。我有一个控制器,需要验证(使用Desive gem)。我添加了Desive TestHelper,但无法使其工作 require 'test_helper' class KeysControllerTest < ActionController::TestCase include Devise::TestHelpers fixtures :keys def setup @user = User.create!(

我在为一个奇怪的问题寻找解决方案。我有一个控制器,需要验证(使用Desive gem)。我添加了Desive TestHelper,但无法使其工作

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
需要“测试助手”
类KeyControllerTest'testuser@demomailtest.com',
:password=>MyTestingPassword',
:password\u confirmation=>“MyTestingPassword”
)
在@user中登录
@键=键(:一)
结束
测试“应该得到索引”吗
获取:索引
断言:成功
assert\u not\u nil赋值(:键)
结束
测试“应该得到新的”吗
获取:新
断言:成功
结束
测试“应该创建密钥”吗
断言差异('Key.count')do
post:create,:key=>@key.attributes
结束
assert_重定向_到键路径(赋值(:键))
结束
测试“应该销毁密钥”吗
assert_difference('Key.count',-1)do
删除:destroy,:id=>@key.to_param
结束
断言\u已将\u重定向到密钥\u路径
结束
结束

我在“rake测试”窗口中得到以下输出:

29)故障:
测试应该创建密钥(KeysControllerTest)[/test/functional/keys\u controller\u test.rb:29]:
“Key.count”未更改1。
预料之中,但事实并非如此
.
30)故障:
测试应该销毁密钥(KeysControllerTest)[/test/functional/keys\u controller\u test.rb:37]:
“Key.count”没有改变-1。
预料之中,但事实并非如此
.
31)故障:
测试应该得到索引(KeysControllerTest)[/test/functional/keys\u controller\u test.rb:19]:
预期响应为a,但为
32)故障:
测试应该得到新的(KeysControllerTest)[/test/functional/keys\u controller\u test.rb:25]:
预期响应为a,但为

有人能告诉我,为什么设计不认证?我对AdminController使用了完全相同的过程,它工作得非常完美

您是否将Deave与confirmable一起使用?在这种情况下,仅创建是不够的,您需要使用
@user.confirm!确认用户

第二,为什么要在功能测试中创建用户?在设备中声明您的用户,如下所示(如果您只需要确认,请在中确认):

测试/夹具/用户.yml:

user1: 
id: 1
email: user1@test.eu
encrypted_password: abcdef1
password_salt:  efvfvffdv
confirmed_at: <%= Time.now %>
user_one: confirmed_at: 2015/01/01 编辑:我刚刚看到,在我的应用程序中,designe 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

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

关键是,在fixtures文件(users.yml)中,您需要确保用户是“确认的”(假设您在用户模型中指定了“confirmable”)。例如,将其放入您的users.yml中:

user1: 
id: 1
email: user1@test.eu
encrypted_password: abcdef1
password_salt:  efvfvffdv
confirmed_at: <%= Time.now %>
user_one: confirmed_at: 2015/01/01 用户一: 确认日期:2015/01/01 仅此而已,无需指定其他字段(电子邮件、加密密码等)。

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

sign_in users(:user_one) 登录用户(:user\u one) 然后它就应该工作了

我有一个类似的问题(但使用的是FactoryGirl,而不是Fixture),通过简单地使用
FactoryGirl.create(:user)
而不是
FactoryGirl.build(:user)
就可以解决它


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

太棒了!是的,我正在使用可确认设置。正如你所描述的那样改变它,它就像一个符咒这真的有效吗?我的意思是,designe在将密码放入数据库之前对其进行加密,据我所知,fixture是直接写入数据库的。这意味着解密将失败(因为abcdef1无法正确解密)。任何人都可以为较新版本的Desive添加指向正确答案的指针吗?是的,需要使用内联代码设置加密密码,才能使用加密数据实际更新列。看见