Ruby on rails Rspec失败-呈现_模板:新的<&引用;新"&燃气轮机;但是使用<;[]>;

Ruby on rails Rspec失败-呈现_模板:新的<&引用;新"&燃气轮机;但是使用<;[]>;,ruby-on-rails,rspec,Ruby On Rails,Rspec,当我运行rspec时,出现以下错误。我真的需要一些帮助!我不确定嵌套资源或ajax调用是否导致rspec失败 1) GoalsController GET #new renders the :new template Failure/Error: expect(response).to render_template :new expecting <"new"> but rendering with <[]> # ./spec/controllers/go

当我运行rspec时,出现以下错误。我真的需要一些帮助!我不确定嵌套资源或ajax调用是否导致rspec失败

1) GoalsController GET #new renders the :new template
  Failure/Error: expect(response).to render_template :new
    expecting <"new"> but rendering with <[]>
  # ./spec/controllers/goals_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
goals\u controller.rb

Rails.application.routes.draw do

  resources :strategies, :only => :none do
    resources :goals
  end

  resources :goals, :only => :none do
    resources :objectives
  end
end
class GoalsController < ApplicationController
  respond_to :html, :js

  def new
    @strategy = Strategy.find(params[:strategy_id])
  end

  def index
    @strategy = Strategy.find(params[:strategy_id])
  end

  def create
    @user = User.find(current_user.id)
    @strategy = Strategy.find(params[:strategy_id])
    @goal = @strategy.goals.create(goal_params.merge(
                                      start_date: @strategy.start_date,
                                      end_date: @strategy.end_date,
                                      created_by: @user.id))
    respond_to do |format|
      format.js { }
    end
  end

  private
    def goal_params
      params.require(:goal).permit(:name, :budget)
    end
end
require 'rails_helper'

RSpec.describe GoalsController, type: :controller do
  describe 'GET #new' do
    it "renders the :new template" do
      get :new, strategy_id: 2
      expect(response).to render_template :new
    end
  end

end

就像Sergey Sokolov在评论中建议的那样,尝试删除所有
:only=>:none
在路线上


resources
将为所有CRUD操作生成路由,
:only=>:none
基本上是说您根本不希望生成路由

尝试从resources:goals中删除:only=>:none,:only=>:none doHi@Sergey,我仍然得到完全相同的错误。我猜
:only=>:none
无关紧要,因为我正在测试的
获取新路线是这样的:-
新策略\u目标获取/策略/:策略\u id/goals/NEW(:format)目标#NEW
尝试在RSpec下添加渲染视图。descripe GoalsController,type::controller doHey@SergeySokolov,添加
render\u视图
后,我仍然收到相同的错误。此外,当我直接转到路由时,浏览器上会出现以下错误:-
GoalsController#new缺少此请求格式和变量的模板。request.formats:[“text/html”]request.variant:[]
似乎您没有查看文件views/goals/new.html.erbi@Zhong Zheng,我仍然收到完全相同的错误。我猜
:only=>:none
无关紧要,因为我正在测试的
获取新路径是:-
新策略\u目标获取/策略/:策略\u id/目标/新(:格式)goals#new
当您直接访问该路线/页面时会发生什么?嘿@ZhongZheng我在直接访问路线时遇到此错误
GoalsController#new缺少此请求格式和变体的模板。request.formats:[“text/html”]request.variant:[]
您需要为目标模型创建一个新模板。类似于
views/goals/new.html.erb的东西
Hey@ZhongZheng,我实际上是通过ajax为这个调用渲染了一个部分,所以在控制台中,我会看到这样的东西:-
Started GET”/strategies/2/goals/new“for::1在2017-01-18 08:26:03+0800由GoalsController处理#新为JS参数:{“strategy_id=>”2}Rendering goals/new.js.erb
Rendered goals/_new.html.erb(37.5ms)Rendered goals/new.js.erb(52.2ms)在101ms内完成了200个OK(视图:80.1ms |活动记录:0.6ms)
在这种情况下,我如何修改我的rspec测试以确保测试正确?谢谢