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" => "458286@email.com"
    }
}
{
    "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

我缺少什么以及如何通过功能测试来测试新会话?

4

1 回答 1

0

原来你必须使用集成测试,而不是功能测试。不要问我为什么...

于 2012-05-07T21:34:36.453 回答