Search code examples
ruby-on-rails-4rspec-rails

UnknownFormat in rails spec


I'm fairly new to rails and I'm trying to return a javascript file from a controller action. In TDD fashion I'm trying to write a failing test first and although I've fixed a bunch of reasons it's failed since starting, I'm now up against an error that I'm not sure how to fix. If I run it in a browser, it works. But running the spec I get an error "ActionController::UnknownFormat" pointing to the respond_to do |format| line.

Here's the controller action...

def job_board
    respond_to do |format|
      format.js
    end
  end

And my spec

require 'rails_helper'

RSpec.describe ApiController, :type => :controller do
  describe "when using an invalid url/api key" do
    it "should return error" do
      get :job_board
      expect(response).to have_http_status(400) #bad request
    end
  end
end

And finally my routes.

Rails.application.routes.draw do
  get 'api/job_board' => 'api#job_board', defaults: { format: 'js'}
end

Any idea why the spec is throwing this error? Thanks.


Solution

  • Got it figured out. I had to change the

    get :job_board 
    

    in the spec to

    get :job_board, format: :js