DEV Community

Cover image for Failure/Error: render template: 'path/to/template'
Ken Obara
Ken Obara

Posted on

Failure/Error: render template: 'path/to/template'

My Rails application works for API. When I tested the codes with Rspec, I got the following error.

$ bundle exec rspec
...
Failure/Error: render template: 'path/to/template'
...
Enter fullscreen mode Exit fullscreen mode

Solutions

We can fix this problem by setting the request header for JSON. There are some solutions.

If we want to set it for each testing code, we can use format: :json

RSpec.describe MyController, type: :controller do
  it 'my test' do
    get :index, format: :json
    expect(response).to have_http_status(:ok)
  end
end
Enter fullscreen mode Exit fullscreen mode

If we want to set it for all testing codes at once, we can change config settings.

# spec/rails_helper.rb or spec/spec_helper.rb
RSpec.configure do |config|
  config.before(:each, type: :controller) do
    request.env['CONTENT_TYPE'] = 'application/json'
    request.env['HTTP_ACCEPT'] = 'application/json'
  end

  config.before(:each, type: :request) do
    if defined?(headers)
      headers['Content-Type'] = 'application/json'
      headers['Accept'] = 'application/json'
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more