Reputation: 201
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
Upvotes: 20
Views: 6438
Reputation: 7406
Below is an example of a Sinatra application and an integration (or end-to-end) test written using Minitest
:
app.rb
:
# frozen_string_literal: true
require 'sinatra'
get '/' do
'test'
end
test/integration_test.rb
:
# frozen_string_literal: true
ENV['APP_ENV'] = 'test'
require 'minitest/autorun'
require 'rack/test'
require 'app'
class IntegrationTest < Minitest::Test
include Rack::Test::Methods
def test_request_succeeds
get '/'
assert last_response.ok?
assert_equal 'test', last_response.body
end
private
def app
Sinatra::Application
end
end
To run the tests configure the test rake task and run rake test
.
Upvotes: 0
Reputation: 7212
Should be simple enough.
Given my_app.rb:
require 'rubygems' # Required only for Ruby version prior to 1.9
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
Upvotes: 27
Reputation: 56240
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
Upvotes: 2
Reputation: 15950
Should be as simple as ruby your_app_name.rb
. Actually, this is shown on Sinatra homepage (bottom).
Upvotes: -1