Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra Rspec - testing that a view has rendered

I am writing tests for a Sinatra app that takes input from an API via a gem. Once I have the API response I need to test that the template has correctly rendered. The response of the API will be the HTML of the page that I am loading.

My first instinct was to write a test that looks like this:

describe 'the root path'
  it 'should render the index view' do
    get '/'

    expect(last_response).to render_template(:index)
  end 
end

Unfortunately when I try this I get the following error: undefined method `render_template'

I was wondering if anyone has encountered this problem - it seems like it should be an easy fix, but I can't seem to find any documentation to help with it.

like image 981
Zubatman Avatar asked Sep 25 '22 03:09

Zubatman


2 Answers

I'm currently not testing views at all because of time constraints, but I did have some limited successs with Rack::Test.

In theory you can say:

require 'rack/test'
include Rack::Test::Methods

def app
  Sinatra::Application
end

describe 'it should render the index view' do
  get '/'
  expect(last_response).to be_ok
  expect(last_response.body).to eq(a_bunch_of_html_somehow)
end

If I were to go this road again, since my views are haml, I could implement the a_bunch_of_html_somehow method using a call to Haml::Engine -- but I'm not sure whether that helps you.

I'm lifting this wholesale from the Sinatra site here -- the page is well worth a read.

like image 177
Andy Jones Avatar answered Sep 29 '22 06:09

Andy Jones


We ended up scrapping this approach since it was better handled by integration testing tool suites such as Selenium or Capybara. There is no equivalent that I could find in the basic Sinatra Rspec suite that could do this - it made more sense to move it into a different scope

like image 20
Zubatman Avatar answered Sep 29 '22 05:09

Zubatman