指定(:user)返回nil而不是用户

时间:2016-04-20 14:56:46

标签: ruby-on-rails ruby-on-rails-4

我写的以下测试是:

  test "valid signup information with account activation" do
    get root_path
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: { user_name: "Example1234",
                                            email: "user1234@example.com",
                                            password: "password",
                                            password_confirmation: "password"}
    end
    assert_equal 1, ActionMailer::Base.deliveries.size
    user = assigns(:user)
    assert_not user.activated?
    #Try to log in before activation.
    log_in_as(user)
    assert_not is_logged_in?
    #Invalid activation token
    get edit_account_activation_path("invalidtoken", email: user.email)
    assert_not is_logged_in?
    #Valid token, wrong email
    get edit_account_activation_path(user.activation_token, email: "wrongemail")
    assert_not is_logged_in?
    #Valid activation token
    get edit_account_activation_path(user.activation_token, email: user.email)
    assert user.reload.activated?
    follow_redirect!
    assert_template 'recipes/index'
    assert is_logged_in?
  end

我收到以下错误:

ERROR["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.4278587700100616]
 test_valid_signup_information_with_account_activation#UsersSignupTest (1.43s)
NoMethodError:         NoMethodError: undefined method `activated?' for nil:NilClass
            test/integration/users_signup_test.rb:30:in `block in <class:UsersSignupTest>'

注意第30行是第一个assert_not user.activated?

似乎分配(:用户)没有得到实际用户,我似乎无法弄清楚原因。

我正在关注Hartl的教程,这是Chapter 10中的测试。用户未返回的任何想法

修改

这是我的控制器代码:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user, only: [:edit, :update]
  .....

   def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.save
        @user.send_activation_email
        format.html { redirect_to root_url notice: 'Please check your email to activate.' }
        format.json { head :no_content }
      else
        format.html { render 'new' }
        format.json { render json: @user.errors }
      end
    end
  end
  ...
end

这是我的帐户激活控制器。

class AccountActivationsController < ApplicationController

  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated! Welcome!."
      redirect_to recipes_url
    else
      flash[:danger] = "Invalid activation link."
      redirect_to root_url
    end
  end
end

2 个答案:

答案 0 :(得分:2)

OK!所以我最终想到了这个。

我仍然不确定为什么,但我将测试中的代码从:post_via_redirect更改为只是一个帖子,它运行正常。

我这样做主要是因为我注意到documentation中的分配(:)它是在帖子而不是post_via_redirect之后发现的。

答案 1 :(得分:0)

我认为您的错误不在您的用户控制器中,而是在您的account_activation控制器中:

class AccountActivationsController < ApplicationController

 def edit
     user = User.find_by(email: params[:email])
   if user && !user.activated? && user.authenticated?(:activation,params[:id])
  user.activate....

更新错误可能在测试中

从教程

test "valid signup information with account activation" do
get signup_path #You have get root_path here!!!
assert_difference 'User.count', 1 do
  post users_path, user: { name:  "Example User",
                           email: "user@example.com",
                           password:              "password",
                           password_confirmation: "password" }
end

signup_path是包含user.activated?

的方法