NoMethodError:未定义的方法' user'为零:NilClass

时间:2015-07-27 15:40:49

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

我正在阅读这本书APIs on Rails并且在第5章中试图测试会话控制器。我收到了以下错误,似乎无法追踪它。有没有一种很好的方法来追捕这类错误?我错过了什么?

 1) Api::V1::SessionsController POST #create when the credentials are correct returns the user record corresponding to the given credentials
 Failure/Error: post :create, { session: credentials }
 NoMethodError:
   undefined method `user' for nil:NilClass

App位于Rails 4.0.2,Ruby 2.2.1

这是我的测试:

require 'spec_helper'

describe Api::V1::SessionsController do

  describe "POST #create" do

    before(:each) do
      @user = FactoryGirl.create :user
    end

    context "when the credentials are correct" do
      puts @user

      before(:each) do
        credentials = { email: @user.email, password: "12345678" }
        post :create, { session: credentials }
      end

      it "returns the user record corresponding to the given credentials" do
        @user.reload
        expect(json_response[:auth_token]).to eql @user.auth_token
      end

      it { should respond_with 200 }
    end


  end


end

以下是会话控制器:

class Api::V1::SessionsController < ApplicationController
  respond_to :json

  def create
    user_password = params[:session][:password]
    user_email = params[:session][:email]
    user = user_email.present? && User.find_by(email: user_email)

    if user.valid_password? user_password
      sign_in user, store: false
      user.generate_authentication_token!
      user.save
      render json: user, status: 200, location: [:api, user]
    else
      render json: { errors: "Invalid email or password" }, status: 422
    end
  end

end

用户控制器:

class Api::V1::UsersController < ApplicationController
  respond_to :json

  def show
    respond_with User.find(params[:id])
  end

  def create
    user = User.new(user_params)
    if user.save
      render json: user, status: 201, location: [:api, user]
    else
      render json: { errors: user.errors }, status: 422
    end
  end

  def update
    user = User.find(params[:id])

    if user.update(user_params)
      render json: user, status: 200, location: [:api, user]
    else
      render json: { errors: user.errors }, status: 422
    end
  end

  def destroy
    user = User.find(params[:id])
    user.destroy
    head 204
  end

  private
    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end


end

routes.rb:

require 'api_constraints'

MarketPlaceApi::Application.routes.draw do
  mount SabisuRails::Engine => "/sabisu_rails"
  devise_for :users
  # Api definition
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/'  do
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      resources :users, :only => [:show, :create, :update, :destroy]
      resources :sessions, :only => [:create, :destroy]
    end
  end
end

用户模型:

class User < ActiveRecord::Base
  validates :auth_token, uniqueness: true


  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  before_create :generate_authentication_token!

  def generate_authentication_token!
    begin
      self.auth_token = Devise.friendly_token
    end while self.class.exists?(auth_token: auth_token)
  end
end

3 个答案:

答案 0 :(得分:2)

您是否已将devise_helper添加到spec / rails_helper.rb?

RSpec.configure do |config|
  ...
  config.include Devise::TestHelpers, type: :controller
  ...
end

答案 1 :(得分:0)

您有用户工厂吗?

FactoryGirl.define do
  factory :user do
    username user
    email user@user.com
    password '12345678'
    password_confirmation { '12345678' }
  end
end

您需要对其进行定义,以便FactoryGirl可以创建用户

答案 2 :(得分:0)

更改

if user.valid_password? user_password

为:

if user and valid_password? user_password

或:

if user && valid_password?(user_password)
在您的sessions_controller.rb文件中