简体   繁体   English

更简单的方法来测试我的简单Rack应用程序?

[英]simpler way to test my simple Rack app?

i have a Rack app like this: 我有一个这样的Rack应用程序:

app = Rack::Builder.new do

    map '/' do
        # ...
    end

    map '/edit' do
        # ...
    end

end.to_app

How would i test it without long-tail installation/setup/learn process. 如果没有长尾安装/设置/学习过程,我将如何测试它。

RSpec and minitest are really great, but i do not really want to learn nor setup them. RSpec和minitest真的很棒,但我真的不想学习也不想设置它们。

Is there something i cat just plug in and write/run tests right away in plain Ruby? 有没有东西我只是插入并在普通的Ruby中立即编写/运行测试?

I want to write tests as simple as i wrote the app above, without advanced techniques and gotchas. 我想编写测试就像我上面写的应用程序一样简单,没有先进的技术和陷阱。

In KISS I Trust! 在KISS I Trust!

Simplest? 简单? Use Rack::Test with Test::Unit . 使用Rack::Test with Test::Unit gem install rack-test and run with ruby filename.rb 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

Update : RSpec style requested - gem install rspec ; 更新 :请求RSpec样式 - gem install rspec ; run with rspec filename.rb 使用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

you can try Specular + Sonar bundle. 你可以试试Specular + Sonar包。

Specular is for writing tests anywhere you need them. Specular用于在任何需要的地方编写测试。

Sonar is a mock "browser" that communicate to your app, just like rack-test does, but with some unique features and simpler workflow. Sonar是一个模拟“浏览器”,可以与您的应用程序进行通信,就像rack-test一样,但具有一些独特的功能和更简单的工作流程。

Using them is as simple as: 使用它们很简单:

...
app.to_app

Spec.new do
  include Sonar
  app(app)

  get
  check(last_response.status) == 200
  # etc...
end
puts Specular.run

so you put your specs right beside your app and write tests quickly in plain Ruby, without having to learn anything. 因此,您可以将您的规范放在应用程序旁边,并在纯Ruby中快速编写测试,而无需学习任何东西。

see the full example running at CIBox 查看在CIBox上运行完整示例

(if it does not run automatically, click Run button) (如果它没有自动运行,请单击“运行”按钮)

PS: writing Rack apps this way is kinda a pain. PS:以这种方式编写Rack应用程序有点痛苦。

You can try a mapper, like Appetite one. 你可以尝试一个mapper,比如Appetite one。

so your app may look like this: 所以你的应用可能看起来像这样:

class App < Appetite
  map :/

  def index
    'index'
  end

  def edit
    'edit'
  end
end

see the same example but with app built by Appetite here 看到相同的例子,但在这里由Appetite建立的应用程序

您可以使用机架测试,但这又需要使用minitest / unit测试,但这是测试Rack应用程序的最常用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM