2012-05-04 49 views
1

這裏是我的測試:我怎麼制定會議#創建測試通過

需要「test_helper」

class SessionsControllerTest < ActionController::TestCase 
    setup do 
    @request.env["devise.mapping"] = Devise.mappings[:user] 
    @u = Factory :user, :password => :mypass, :password_confirmation => :mypass 
    end 
    test 'log in page loads' do 
    get :new 
    assert :success 
    end 
    test 'log in with devise password' do 

    post :create, :user => {:email => @u.email, :password => 'mypass'} 
    ap session 
    end 

end 

給出了這樣的輸出,這表明在簽收失敗:

Loaded suite test/functional/sessions_controller_test 
Started 
.{ 
     "action" => "create", 
     "locale" => "en", 
    "controller" => "sessions", 
      "user" => { 
     "password" => "mypass", 
      "email" => "[email protected]" 
    } 
} 
{ 
    "flash" => { 
     :alert => "Invalid email or password." 
    } 
} 
. 
Finished in 0.49123 seconds. 

這是我的會話控制器:

#this is an extension of the devise controller for sessions 
class SessionsController < Devise::SessionsController 
    before_filter :set_title_h1, :only => :new 
    before_filter :debug, :only => :create 
    before_filter :old_password_system_fix, :only => :create 

    private 

    def set_title_h1 
    @layout[:show_h1] = false 
    title 'Sign in Or Register' 
    end 
    def after_sign_in_path_for(resource) 
    #override Devise default sign in path /opt/local/lib/ruby/gems/1.8/gems/devise-1.1.2/lib/devise/controllers/helpers.rb 
    #edit_user_registration_path 
    '/en/main/index' #forces locale to be defined 
    end 

    def after_sign_out_path_for(resource) 
    #override Devise default sign out path /opt/local/lib/ruby/gems/1.8/gems/devise-1.1.2/lib/devise/controllers/helpers.rb 
    main_index_path 
    end 
    def old_password_system_fix 
    #purpose is to bring old users into the new system by setting their old password to the new format 

    require 'digest/md5' 

    email = params[:user][:email] 
    pw = params[:user][:password] 

    #get user 
    u = User.find_by_email email 

    return if u.nil? 

    #if they don't have a devise-style pw, authenticate with old 
    if u.encrypted_password.blank? && u.old_password.present? 

     #if [params pw] == md5 [old pw] then create devise-style pw & salt, store it, and let them through to devise auth action 
     if u.old_password == Digest::MD5.hexdigest(pw) 

     set_devise_style_pw(u, pw) 

     #if no match, give "invalid email or pw" message. 
     else 
     #flash[:notice] = "Sign in failed." 
     flash[:notice] = t 'devise.failure.invalid' 
     #render :new 
     redirect_to new_user_session_path 
     end 

    end 
    end 
    def debug 
    ap params 
    end 
end 

我錯過了什麼,如何通過功能測試來測試新的會話?

+1

我不是專家(我有我自己的設計測試問題掛在那裏),但似乎有一個'sign_in'測試助手,你可能想嘗試,而不是'post'ing。看(如果你還沒有): https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec% 29 –

+1

sign_in的作品,但它繞過會議控制器。我真的需要測試會話控制器,因爲它有一個非標準的創建操作,它覆蓋了Devise默認設置。但是,即使使用有效的用戶,我也無法實際登錄... – pixelearth

回答

0

原來你必須使用集成測試,而不是功能測試。不要問我爲什麼...