RSPEC не отображает файл JSON.jbuilder (ошибка отсутствия шаблона)

Я тестирую свое приложение rails с помощью RSpec и тестирую контроллер. В частности, я пытаюсь отобразить файл представления jobs.json.jbuilder.

Мой контроллер

class CustomersController < ApplicationController
  before_action :authenticate_member!
  before_action :set_customer


def jobs
    @jobs = @customer.jobs
end

Мой соответствующий вид находится в: views/customers/jobs.json.jbuilder

Мой тест RSpec:

describe "GET job" do 
    it "renders the jobs view" do 
    get 'jobs', {id: @customer.id}, {format: :json}
    expect(response).to render_template("customers/jobs.json.jbuilder")
    end
end

Однако ошибка, которую я продолжаю получать:

     Failure/Error: get :jobs, {id: @customer.id}, {format: :json}
     ActionView::MissingTemplate:
       Missing template customers/jobs, application/jobs with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :slim]}. Searched in:
         * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007fa219178f78>"
     # ./spec/support/engine_controller.rb:25:in `process_action'
     # ./spec/support/engine_controller.rb:3:in `get'
     # ./spec/controllers/customers_controller_spec.rb:31:in `block (3 levels) in <top (required)>'

ПРИМЕЧАНИЕ. Если я создам файл jobs.html.erb, то эта ошибка исчезнет (и он отобразит файл jobs.html.erb) — поэтому мой вопрос: как мне отобразить представление .json.jbuilder?

Вот мой файл spec/support/engine_controller:

 module EngineController
   def get(action, parameters = nil, session = nil, flash = nil)
           process_action(action, parameters, session, flash, "GET")
   end 

    private
       def process_action(action, parameters = nil, session = nil, flash =       nil, method = "GET")
        parameters ||= {}
        process(action, method, parameters.merge!(:use_route => :my_engine_name), session, flash, )
      end

person TABISH KHAN    schedule 20.04.2015    source источник


Ответы (2)


Устранена проблема с использованием этого синтаксиса:

describe "GET job" do 
  it 'renders the jobs view' do 
    get 'jobs', { id: @customer.id, format: :json }
    expect(response).to render_template("customers/jobs")
  end
end
person TABISH KHAN    schedule 21.04.2015

Это очень поздний ответ, но для тех, кто все еще задается этим вопросом...

Для рендеринга json.jbuilder по умолчанию вы должны прописать его в route.rb

в таком случае...

resources :costumers, defaults: { format: :json }, only: [...] do
  get 'job', to: 'costumers#job'
end
person Dennys Azevedo    schedule 30.03.2019