我正在尝试我的第一次单元测试,我收到以下错误
失败:
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呈现索引模板Failure / Error: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 endI'm currently attempting my first unit test and I'm receiving the following errors
Failures:
1) StaticPagesController GET #index responds successfully with an HTTP 200 status code Failure/Error: 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 renders the index template Failure/Error: 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)>'Here is my unit test code:
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 endAnd here is my static_controller.rb code:
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 endWhy do are these errors coming up and how do I fix the problem? I've only been coding for a few months so any assistance would be much appreciated. Thanks :)
Update
Here is my application controller
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 endHere is my user_mailer code as well
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最满意答案
你的问题是这一行:
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-(和-RSpec)#controller-specs 。 我没有在答案中添加这些步骤,因为我相信你会选择第一种情况(因为每个页面都需要一些没有会话的页面,至少对于登录页面而言)。
Your problem is this line:
before_action :authenticate_user!This makes devise to check authorisation for current_user, which is nil in your test. There are two ways to fix it depending on what your requirements are.
Firstly, if you want any internet user to be able to view your static pages without login, add:
skip_before_action :authenticate_user!in your StaticPagesController. This will tell devise that you don't require current_user to be allowed to view the page (not really - it doesn't tell devise anything, it just not asking it to authorise user).
Second option - you need user to be logged in to view those pages. In this case you need to create fake session before you start your test using some helper methods provided by devise. This is very easy and well documented process, you can find the steps here: https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)#controller-specs. Not adding those steps in the answer as I believe you will go with first case (since every page requires some pages available without the session, at least for the login page).
更多推荐
发布评论