林繼軌道4在行動指南和Im高達第7章RSpec的測試失敗,但應用程序的工作
這是錯誤林錯過:
± bundle exec rspec
.F........................
Failures:
1) Admin::UsersController GET #index returns http success
Failure/Error: get :index
NoMethodError:
undefined method `authenticate!' for nil:NilClass
# ./app/controllers/admin/application_controller.rb:10:in `authorize_admin!'
# ./spec/controllers/admin/users_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 1.2 seconds (files took 1.86 seconds to load)
26 examples, 1 failure
Failed examples:
rspec ./spec/controllers/admin/users_controller_spec.rb:6 # Admin::UsersController GET #index returns http success
./app/controllers/admin/ application_controller.rb看起來是這樣的:
class Admin::ApplicationController < ApplicationController
before_action :authorize_admin!
def index
end
private
def authorize_admin!
authenticate_user!
unless current_user.admin?
redirect_to root_path, alert: "You must be an admin to do that."
end
end
end
./app/controllers/admin/users_controller.rb看起來是這樣的:
class Admin::UsersController < Admin::ApplicationController
def index
@users = User.order(:email)
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:notice] = "User has been created."
redirect_to admin_users_path
else
flash.now[:alert] = "User has not been created."
render "new"
end
end
private
def user_params
params.require(:user).permit(:email, :password)
end
end
./spec/controllers/admin/users_controller_spec.rb看起來是這樣的:
require 'rails_helper'
RSpec.describe Admin::UsersController, type: :controller do
describe "GET #index" do
it "returns http success" do
get :index
expect(response).to have_http_status(:success)
end
end
end
正如你可能會說我是有點出我的深度,任何解決這一幫助將非常感激。
我相信'authenticate_user!'是來自Devise的一部分的Warden? – user1385827