rspecを実行すると、次のエラーが発生します。ここで何か助けを借りて本当にできました!ネストされたリソースやajax呼び出しがrspecの失敗に寄与しているかどうかはわかりません。Rspecエラー - render_template:new expecting <"new">ただし、レンダリングは<[]>
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)>'
ここに私のコードを示します。
routes.rbを
Rails.application.routes.draw do
resources :strategies, :only => :none do
resources :goals
end
resources :goals, :only => :none do
resources :objectives
end
end
goals_controller.rb
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
goals_controller_spec.rb
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
リソース:目標からのみ:>:none: –
こんにちは@Sergey、私はまだまったく同じエラーを取得しています。私は ':=>:none'は問題ではないと思います。私がテストしている' get new route'はこれです: - 'new_strategy_goal GET /strategies/:strategy_id/goals/new(.:format)goals#new' –
RSpec.describe GoalsControllerの下でrender_viewsを追加しようとすると::controller do –