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
Rack testing is a process that tests a construction sealant on specific performance aspects including: Weathering while under compression.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With