Sinatra Basics : Writing a Test using Unit Test framework
Learn how to write a test using test unit framework for a Sinatra application.
Create a new project folder to keep all the files.
Add rack-test gem to Gemfile
gem "rack-test", require: "rack/test"
the entire Gemfile looks like this:
source 'https://rubygems.org' gem 'shotgun' gem 'sinatra' gem "rack-test", require: "rack/test" gem 'test-unit'
Install the gems.
This installed test-unit gem version 3.2 and rack-test gem version 0.6.3 on my system.
Create a test folder inside the project folder and create sample_test.rb in it.
ENV['RACK_ENV'] = 'test' require 'test/unit' require 'rack/test' require File.expand_path '../sample.rb' class SampleTest < Test::Unit::TestCase include Rack::Test::Methods def app Sinatra::Application end def test_sample get '/' assert last_response.ok? assert_equal 'hi', last_response.body end end
Run the test:
bundle exec ruby sample_test.rb
In the project folder, create sample.rb:
require 'sinatra' get '/' do 'hi' end
In this article, you learned how to write a test using TestUnit testing framework to test Sinatra application.
Software Compatibility Best Practices
I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.
You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.