Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rspec 如何测试特定模板是用Sinatra还是Padrino呈现的?_Rspec_Sinatra_Padrino - Fatal编程技术网

Rspec 如何测试特定模板是用Sinatra还是Padrino呈现的?

Rspec 如何测试特定模板是用Sinatra还是Padrino呈现的?,rspec,sinatra,padrino,Rspec,Sinatra,Padrino,假设您有两个模板: # app/views/users/foo.haml.html %p ... # app/views/users/bar.haml.html %p ... 以及呈现以下内容的控制器: MyApp.controllers :users do get '/herp' do render 'users/foo' end get '/derp' do render 'users/bar' end end 编写RSpec测试以断言特定视图由控制器呈

假设您有两个模板:

# app/views/users/foo.haml.html
%p ...

# app/views/users/bar.haml.html
%p ...
以及呈现以下内容的控制器:

MyApp.controllers :users do
  get '/herp' do
    render 'users/foo'
  end
  get '/derp' do
    render 'users/bar'
  end
end
编写RSpec测试以断言特定视图由控制器呈现的最佳方法是什么

理想情况下,是否有一种方法可以让测试只检查视图是否已呈现,而不实际呈现它?

您可以使用以下方法检查页面的呈现:

RSpec.configure do |config|
  config.include Rack::Test::Methods
  # other stuff too…
end


describe "Getting the user's page", :type => :request do
  let(:username) { "herp" }
  before do
    get "/users/#{username}"
  end
  subject{ last_response }
  its(:status) { should == 200 }
  its(:body) { should include "Something that was on the template" }
end
一旦这起作用,您就可以对其进行泛化,生成不同的用户名来运行该规范等等


要检查页面是否在不呈现的情况下呈现,也许您可以将
render
方法加倍?

我不想将测试设置为字符串匹配,因为这太脆弱了:如果视图更改,测试应该仍然有效,因为控制器中没有任何内容被更改。我想可以在
渲染时加倍,我没有想到这一点。这可能是最好的方法。@JohnFeminella好吧,渲染输出毕竟是一个字符串:)您可能还想对模板运行某种有效性检查,因为如果不实际运行它,您将不知道模板的问题是否会导致渲染器出错。我同意这也是一个字符串匹配,但是当我编辑视图的内容时,此字符串不会更改。:)@我的观点是,有时测试需要与特定的东西联系起来,即使它引入了脆性,因为它就是这样。我经常介绍这种规格,你必须在某个地方付钱:)