Tanya
Tanya

Reputation: 38

How do I write an rspec test for a controller method that only renders html?

My routes.rb:

Rails.application.routes.draw do
  root 'application#hello'
end

My application_controller.rb:

class ApplicationController < ActionController::Base
  def hello
    render html: "Hello, world!"
  end
end

I have tried this spec but it does not work:

RSpec.describe ApplicationController do
  render_views

  describe '#hello' do
    it 'renders hello world' do
      get :hello_world

      expect(response.body).to include("Hello, world!")
    end
  end
end

I get this error:

Failures:

  1) ApplicationController#hello renders hello world
     Failure/Error: get :hello_world

     ActionController::UrlGenerationError:
       No route matches {:action=>"hello_world", :controller=>"application"}

How do I write an Rspec test to test that this method renders this html?

Upvotes: 0

Views: 328

Answers (1)

Subash
Subash

Reputation: 3168

The error is saying that it can't find the route hello_world, try changing

it 'renders hello world' do
  get :hello_world

  expect(response.body).to include('hello world')
end

to

it 'renders hello world' do
  get :hello

  expect(response.body).to include('hello world')
end

and see if it helps

Upvotes: 1

Related Questions