Controller spec when controller method is being hit through AJAX

Public, Testing / TDD

harrylevine

Created: Jul 29, 2016     Updated: Jul 29, 2016


I had a controller method that is being called via AJAX. This method does not specifically render any pages (i.e. update.html.erb). The controller spec was throwing a missing template error

Failure/Error: put :update,
ActionView::MissingTemplate:
  Missing template users/attendance_summaries/update, application/update with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :prawn, :prawn_dsl]}. Searched in:
    * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007ff0bd51c308>"
    * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007ff0b4ee6400>"

The solution was to add format: :js to the spec, like this:

it "sets the acknowledged_by to the manager's ID" do
  put :update,
    format: :js,
    user_attendance_summary: { reason: 'xyz', acknowledge_reason: 'qrs'},
    user_id: hourly_employee.id.to_s,
    id: UserAttendanceSummary.first.id.to_s

  expect(UserAttendanceSummary.first.acknowledged_by).to eq manager.id
end