stevenspiel
stevenspiel

Reputation: 5999

rspec controller test can't find partial

I'm using render_to_string as a json response. It works fine in my app, but rspec can't find the partial.

controller:

# 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:

# 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

I get the error:

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"

How can I get rspec to recognize the partial?

Upvotes: 3

Views: 513

Answers (1)

stevenspiel
stevenspiel

Reputation: 5999

Ok, I figured it out. Turns out I needed to add the extension to the partial and it all works fine.

The controller needed to be

form = render_to_string('_user_form.html.erb', layout: false)

Upvotes: 2

Related Questions