我目前正在尝试进行首次单元测试,但我收到以下错误
故障:
1)StaticPagesController GET #index使用HTTP 200状态代码成功响应 失败/错误:get:index
NoMethodError:
undefined method `authenticate!' for nil:NilClass
# /Users/danielmayle/.rvm/gems/ruby-2.2.1/gems/devise-3.5.2/lib/devise/controllers/helpers.rb:112:in `authenticate_user!'
# ./spec/static_pages_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
2)StaticPagesController GET #index呈现索引模板 失败/错误:get:index
NoMethodError:
undefined method `authenticate!' for nil:NilClass
# /Users/danielmayle/.rvm/gems/ruby-2.2.1/gems/devise-3.5.2/lib/devise/controllers/helpers.rb:112:in `authenticate_user!'
# ./spec/static_pages_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
这是我的单元测试代码:
require 'rails_helper'
describe StaticPagesController, :type => :controller do
context "GET #index" do
before do
get :index
end
it "responds successfully with an HTTP 200 status code" do
expect(response).to be_success
expect(response).to have_http_status(200)
end
it "renders the index template" do
expect(response).to render_template("index")
end
end
end
这是我的static_controller.rb代码:
class StaticPagesController < ApplicationController
def index
end
def landing_page
@featured_product = Product.first
end
def thank_you
@name = params[:name]
@email = params[:email]
@message = params[:message]
UserMailer.contact_form(@email, @name, @message).deliver_now
end
end
为什么会出现这些错误,如何解决问题?我只编写了几个月的代码,所以我们非常感谢任何帮助。谢谢:)
更新
这是我的应用程序控制器
class ApplicationController < ActionController::Base
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
redirect_to main_app.root_url, :alert => exception.message
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
这是我的user_mailer代码
class UserMailer < ActionMailer::Base
default from: "dmayle012@gmail.com"
def contact_form(email, name, message)
@message = message
mail(:from => email,
:to => 'dmayle012@gmail.com',
:subject => "New ActionMail Message from #{name}")
end
end
答案 0 :(得分:0)
你的问题就在这一行:
before_action :authenticate_user!
这使得设计检查current_user
的授权,在您的测试中为零。根据您的要求,有两种方法可以解决它。
首先,如果您希望任何互联网用户无需登录即可查看您的静态页面,请添加:
skip_before_action :authenticate_user!
在您的StaticPagesController中。这将告诉设计你不要求current_user被允许查看页面(不是真的 - 它没有告诉设计任何东西,它只是没有要求它授权用户)。
第二个选项 - 您需要用户登录才能查看这些页面。在这种情况下,您需要在使用devise提供的一些帮助方法开始测试之前创建假会话。这是一个非常简单且记录良好的过程,您可以在此处找到以下步骤:https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)#controller-specs。我没有在答案中添加这些步骤,因为我相信你会选择第一种情况(因为每个页面都需要一些没有会话的页面,至少对于登录页面而言)。
答案 1 :(得分:0)
Devise有一些专门针对此的辅助方法。以下是如何设置控制器规范以超越undefined method authenticate_user! for nil
错误
首先,您需要将它们包含在您的rails_helper.rb中,如此
# spec/rails_helper.rb
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
end
然后你可以在静态页面控制器中使用这样的帮助器
# spec/controllers/static_pages_controller_spec.rb
describe StaticPagesController, :type => :controller do
context "GET #index" do
before :each do
user = FactoryGirl.create(:user, approved: true)
sign_in :user, user
get :index
end
...
end
end