[英]simpler way to test my simple Rack app?
我有一个这样的Rack应用程序:
app = Rack::Builder.new do
map '/' do
# ...
end
map '/edit' do
# ...
end
end.to_app
如果没有长尾安装/设置/学习过程,我将如何测试它。
RSpec和minitest真的很棒,但我真的不想学习也不想设置它们。
有没有东西我只是插入并在普通的Ruby中立即编写/运行测试?
我想编写测试就像我上面写的应用程序一样简单,没有先进的技术和陷阱。
在KISS I Trust!
简单? 使用Rack::Test
with Test::Unit
。 gem install rack-test
并使用ruby filename.rb
运行
require "test/unit"
require "rack/test"
class AppTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Rack::Builder.new do
map '/' do
run Proc.new {|env| [200, {'Content-Type' => 'text/html'}, "foo"] }
end
map '/edit' do
# ...
end
end.to_app
end
def test_index
get "/"
assert last_response.ok?
end
end
更新 :请求RSpec样式 - gem install rspec
; 使用rspec filename.rb
运行
require 'rspec'
require 'rack/test'
describe 'the app' do
include Rack::Test::Methods
def app
Rack::Builder.new do
map '/' do
run Proc.new {|env| [200, {'Content-Type' => 'text/html'}, "foo"] }
end
map '/edit' do
# ...
end
end.to_app
end
it 'says foo' do
get '/'
last_response.should be_ok
last_response.body.should == 'foo'
end
end
Specular
用于在任何需要的地方编写测试。
Sonar
是一个模拟“浏览器”,可以与您的应用程序进行通信,就像rack-test
一样,但具有一些独特的功能和更简单的工作流程。
使用它们很简单:
...
app.to_app
Spec.new do
include Sonar
app(app)
get
check(last_response.status) == 200
# etc...
end
puts Specular.run
因此,您可以将您的规范放在应用程序旁边,并在纯Ruby中快速编写测试,而无需学习任何东西。
(如果它没有自动运行,请单击“运行”按钮)
PS:以这种方式编写Rack应用程序有点痛苦。
你可以尝试一个mapper,比如Appetite
one。
所以你的应用可能看起来像这样:
class App < Appetite
map :/
def index
'index'
end
def edit
'edit'
end
end
看到相同的例子,但在这里由Appetite
建立的应用程序
您可以使用机架测试,但这又需要使用minitest / unit测试,但这是测试Rack应用程序的最常用方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.