4

I am testing my rails application using RSpec, and am testing the controller. Specifically, I am trying to render my jobs.json.jbuilder view file.

My Controller

class CustomersController < ApplicationController
  before_action :authenticate_member!
  before_action :set_customer


def jobs
    @jobs = @customer.jobs
end

My corresponding view is located in : views/customers/jobs.json.jbuilder

My RSpec Test is:

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

However the error I keep getting is:

     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)>'

NOTE: If I make a jobs.html.erb file.. then this error goes away, (and it renders the jobs.html.erb file ) - so my question is how do i render the .json.jbuilder view ?

Here is my spec/support/engine_controller file:

 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
Felixyz
  • 19,053
  • 14
  • 65
  • 60
TABISH KHAN
  • 1,553
  • 1
  • 19
  • 32

3 Answers3

4

Fixed the problem by using this syntax:

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
mmsilviu
  • 1,211
  • 15
  • 25
TABISH KHAN
  • 1,553
  • 1
  • 19
  • 32
2

It's a very late answer, but, for those who are still coming to this question...

For rendering json.jbuilder by default you should put it on routes.rb

in this case...

resources :costumers, defaults: { format: :json }, only: [...] do
  get 'job', to: 'costumers#job'
end
1

worth noting that adding

config.render_views = true

to the RSpec.configure in the rails_helper is also needed

as per stackoverflow answer