get:index Rspec错误,在静态索引控制器页面的单元测试期间(get :index Rspec error, during unit test for Static Index contro

系统教程 行业动态 更新时间:2024-06-14 16:59:47
get:index Rspec错误,在静态索引控制器页面的单元测试期间(get :index Rspec error, during unit test for Static Index controller page)

我正在尝试我的第一次单元测试,我收到以下错误

失败:

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 end

I'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 end

And 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 end

Why 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 end

Here 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).

更多推荐

本文发布于:2023-04-17 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ae7d35d12ff778b10bc9b8af98649bc4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   静态   索引   单元测试   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!