20

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

Tony M.
  • 201
  • 2
  • 3

3 Answers3

27

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
irosenb
  • 828
  • 2
  • 13
  • 26
psyho
  • 7,142
  • 5
  • 23
  • 24
  • I appreciate for the solution. Could you please tell me why do we use method 'app' here? – palash-kulkarni May 28 '17 at 17:21
  • @palash-kulkarni the app method is for referencing the Class name of the Sinatra app, or an rack-ready app. So in this example the test_hi_returns_hello_world actually is invoked using test_hi_returns_hello_world(app) – Waheed Mar 09 '19 at 14:04
2

I posted a small example based on psyho's answer. I also added ActiveRecord support, including test fixtures.

I configured rake to run the tests:

# Rakefile
require_relative './app'
require 'rake'
require 'rake/testtask'
require 'sinatra/activerecord/rake'

Rake::TestTask.new do |t|
  t.pattern = "test/**/*_test.rb"
end

task default: :test

Now I can run the tests like this:

rake

An example test looks like this:

require_relative '../test_helper'

class BlogTest < ActiveSupport::TestCase
  fixtures :blogs

  def test_create
    blog = Blog.create(:name => "Rob's Writing")
    assert_equal "Rob's Writing", blog.name
  end

  def test_find
    blog = Blog.find_by_name("Jimmy's Jottings")
    assert_equal "Stuff Jimmy says", blog.tagline
  end
end

I require this helper in every test file to wire everything up:

# test_helper.rb
require_relative '../app'
require 'minitest/autorun'
require 'active_record'
require 'rack/test'

ActiveRecord::Base.establish_connection(:test)

#Set up fixtures and such
class ActiveSupport::TestCase
  include ActiveRecord::TestFixtures
  include ActiveRecord::TestFixtures::ClassMethods
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  self.fixture_path = 'test/fixtures'
  self.use_transactional_fixtures = true
  self.use_instantiated_fixtures  = false
end
Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
-1

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

Damir Zekić
  • 15,630
  • 2
  • 34
  • 35
  • 1
    Thanks for replying. I guess I was not clear - how do I run the Rack::Test test file that I create. – Tony M. Jan 13 '10 at 18:34