我正在使用render_to_string
作为json响应。它在我的应用程序中工作正常,但rspec找不到部分。
控制器:
# controllers/admin_controller.rb
class AdminController < ApplicationController
def retrieve_edit_form
user = User.find(params[:id])
form = render_to_string('_user_form', layout: false)
respond_to do |format|
format.json { render json: { form: form } }
end
end
end
规格:
# spec/app/controllers/admin_controller_spec.rb
require 'rails_helper'
RSpec.describe AdminController, type: :controller do
render_views
describe 'GET retrieve_edit_form' do
it 'returns http success' do
get :retrieve_edit_form, id: 100, format: :json
expect(response).to be_success
end
end
end
我收到错误:
Missing template admin/_user_form, application/_user_form with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
* "/Users/.../app/views"
如何让rspec识别部分?
答案 0 :(得分:2)
好的,我明白了。结果我需要将扩展添加到局部,这一切都正常。
控制器需要
form = render_to_string('_user_form.html.erb', layout: false)