How do you run tests in Sinatra?

Viewed 6056

I have no idea how to test my Sinatra application. Do I just run

ruby

That does not seem to work. All files out there only talk about how to write the contents of the file, but not about how to get it running.

Thanks

3 Answers

Should be simple enough.

Given my_app.rb:

require 'rubygems'
require 'sinatra'

get '/hi' do
  "Hello World!"
end

And my_app_test.rb:

require 'my_app'
require 'test/unit'
require 'rack/test'

set :environment, :test

class MyAppTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  def test_hi_returns_hello_world
    get '/hi'
    assert last_response.ok?
    assert_equal 'Hello World!', last_response.body
  end
end

You should make sure you have right gems installed:

gem install sinatra rake rack-test

Now you can run your application and tests like this:

ruby my_app.rb
ruby my_app_test.rb

Should be as simple as ruby your_app_name.rb. Actually, this is shown on Sinatra homepage (bottom).

Related