2011-04-10 45 views
9

我正在寻找一个奇怪问题的解决方案。我有一个控制器,需要认证(与设计宝石)。我添加了设计TestHelpers,但我无法得到它的工作。设计认证的Ruby on Rails功能测试

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 

和我在我的“耙测试”窗口中下面的输出:

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使用完全相同的过程,它的工作原理非常完美。

回答

23

您是否在使用Devise进行确认?在这种情况下,创建是不够的,你需要确认用户与@user.confirm!

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

测试/夹具/ users.yml里:

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

,并与他们签名在您的功能测试:

sign_in users(:user1) 

编辑:我刚才看到,在我的应用程序的设计,Testhelpers在测试/测试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 
+0

太棒了!是的,我正在使用可确认的设置。既然像你描述的那样改变它,它就像一个魅力。 :) – axx 2011-04-10 16:35:52

+1

这真的有用吗?我的意思是,设计在将密码放入数据库之前对密码进行加密,并且据我所知,夹具直接写入数据库。这意味着解密应该失败(因为abcdef1不能正确解密)。 – 2011-10-27 11:34:43

+0

陈旧。不适用于较新的设计。 – oma 2012-02-23 12:53:51

1

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

问题是,在你的灯具文件(users.yml)中,你需要确保用户'确认'(假设你在用户模型中指定了“可确认”)。所以,举例来说,把这个在您的users.yml里:

 
user_one: 
    confirmed_at: 2015/01/01 

这一切,无需指定其他字段(电子邮件,加密密码等)。

现在在你的控制器测试(例如在“设置”或“前”)你只需代码:

 
sign_in users(:user_one) 

然后它应该只是工作!

0

我有一个类似的问题(但使用FactoryGirl,而不是灯具),并能够通过简单地使用FactoryGirl.create(:user)而不是FactoryGirl.build(:user)来解决它。

很明显,Devise要求用户必须坚持到数据库,以便一切正常工作。