如何在 rspec rails6 中获取/发布方法

问题描述

刚接触 Rails

我正在使用 RSpec gem 进行测试

这是我的控制器

class QuestionsController < ApplicationController

  before_action :require_user,except: %i[show index]

  before_action :authorize_user,only: %i[edit update destroy]
  def index
    @questions = Question.all

  end

  def show
    @answers = Answer.where('question_id = :question_id ',{ question_id: params[:id] })
    @count = Answer.where('question_id = :question_id ',{ question_id: params[:id] }).count
    $count_global = @count
    session[:question_id] = params[:id]
    @questions = Question.all
  end

  def new
    @question = Question.new
  end


  def edit; end
  def create
    @question = Question.new(question_params)
    @question.user_id *strong text*= current_user.id
    @question.save

    redirect_to root_path
  end

end

这是spec/requests/question_spec.rb

  describe "GET /new" do
    it "renders a successful response" do
      get new_question_url
      expect(response).to be_successful
    end
  end

测试未通过 错误

Failure/Error: expect(response).to be_successful
       expected `#<Actiondispatch::TestResponse:0x0000558594f082e0 @mon_data=#<Monitor:0x0000558594f08290>,@mon_data_...,@method=nil,@request_method=nil,@remote_ip=nil,@original_fullpath=nil,@fullpath=nil,@ip=nil>>.successful?` to be truthy,got false

有人可以帮忙解决这个问题吗

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)