2011-04-10 87 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要求用戶必須堅持到數據庫,以便一切正常工作。