Sinatra Rspec-测试视图是否已呈现

Zub*_*Man 5 testing rspec sinatra

我正在为Sinatra应用编写测试,该应用通过gem从API接收输入。收到API响应后,我需要测试模板是否正确呈现。API的响应将是我正在加载的页面的HTML。

我的第一个直觉是编写一个如下所示的测试:

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

    expect(last_response).to render_template(:index)
  end 
end
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我尝试这样做时,出现以下错误: undefined method `render_template'

我想知道是否有人遇到过这个问题-似乎应该很容易解决,但是我似乎找不到任何文档来解决这个问题。

And*_*nes 3

由于时间限制,我目前根本没有测试视图,但我确实使用 Rack::Test 取得了一些有限的成功。

理论上你可以说:

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
Run Code Online (Sandbox Code Playgroud)

如果我再次走这条路,因为我的观点是 haml,我可以a_bunch_of_html_somehow使用调用来实现该方法Haml::Engine-- 但我不确定这是否对你有帮助。

我从 Sinatra 网站批量转载了这篇文章——该页面非常值得一读。