必须在sessions_controller.rb中使用实例变量来传递测试

时间:2016-11-01 00:26:05

标签: ruby-on-rails railstutorial.org

执行Hartl Rails教程示例应用程序。他的sessions_controller.rb使用局部变量,但为了传递测试,我必须将它们转换为实例变量。我搞砸了其他什么吗?

我的sessions_controller.rb:

    class SessionsController < ApplicationController
  def new

  end

  def create
    @user = User.find_by(email: params[:session][:email].downcase)
    if @user && @user.authenticate(params[:session][:password])
      if @user.activated?
        log_in @user
        params[:session][:remember_me] == '1' ? remember(@user) : forget(@user)
        redirect_back_or @user
      else
        message = "Account not activated. "
        message += "Check your email for the activation link."
        flash[:warning] = message
        redirect_to root_url
      end
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end

end

将@user替换为用户以获取Hartl版本。

来自users_login_test.rb的特定测试:

  test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_equal cookies['remember_token'], assigns(:user).remember_token
  end

测试中实例变量的使用来自Hartl,而不是我。

产生的具体错误是:

ERROR["test_login_with_remembering", UsersLoginTest, 1.3149604040081613]
 test_login_with_remembering#UsersLoginTest (1.31s)
NoMethodError:         NoMethodError: undefined method `remember_token' for nil:NilClass
            test/integration/users_login_test.rb:43:in `block in <class:UsersLoginTest>'

0 个答案:

没有答案