views:

147

answers:

2

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

A: 

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

Damir Zekić
Thanks for replying. I guess I was not clear - how do I run the Rack::Test test file that I create.
Tony M.
+3  A: 

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 rake-test

And now you can run your application and tests like this:

ruby my_app.rb
ruby my_app_test.rb
psyho