Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Sinatra is an excellent lightweight framework for developing web services. It will serve as the application framework for our Service Oriented Architecture's HTTP endpoints.
The testing approach will be in-process, which means that the test suite will run alongside the web service in the same Ruby process. This eliminates the need for an external HTTP web server to be running. Now we will look into Testing in Sinatra.
Testing
Sinatra tests can be written in any Rack-based testing framework or library. Rack:: The following tests are advised:
require 'my_first_sinatra_app'
require 'minitest/autorun'
require 'rack/test'
class MyAppTest < Minitest::Test
include Rack::Test::Methods
def app
Sinatra::Application
end
def test_my_default
get '/'
assert_equal 'Hello Everyone', last_response.body
end
def test_with_params
get '/meet', :name => 'Krishna'
assert_equal 'Hello Krishna', last_response.body
end
def test_with_user_agent
get '/', {}, 'HTTP_USER_AGENT' => 'Laptop'
assert_equal "You're using Laptop", last_response.body
end
end
Note: Replace Sinatra::Application above with the class name of your application if you're using Sinatra in a modular style.
Middleware, libraries and modular apps provided by Sinatra::Base
When creating reusable components like Rack middleware, Rails metal, basic libraries with a server component, or even Sinatra extensions, defining your app at the top level has significant disadvantages. The top-level setup is predicated on a micro-app approach (e.g., a single application file, ./public and ./views folders, logging, exception detail page, etc.). Sinatra: Base comes into the picture:
require 'Sinatra/base'
class MyApp < Sinatra::Base
Set: sessions, true
set: foo, 'bar'
get '/' do
'Hello Everyone'
end
end
Sinatra had the following options: Base subclasses are identical to those offered by the top-level DSL. Most advanced applications can be converted to Sinatra: base elements with the following two modifications:
Otherwise, all of Sinatra's DSL methods are imported into the main namespace. Your code should instead need Sinatra: base.
In a subclass of Sinatra::Base, include the routes, error handlers, filters, and options for your project.
Classic Style vs Modular
Contrary to popular assumption, the traditional look is very acceptable. You do not need to move to a modular application if it works for your application.
There will only be one Sinatra application per Ruby process if you use the conventional style instead of the modular one. Change to the modular style if you intend to utilise more than one. There is no reason why you can't combine the classic and modular styles.
You should be aware of somewhat varied default settings if switching between styles:
A Modular Application Serving
Modular software can be launched in one of two ways, actively starting with the run!
Start with:
ruby app_name.rb
Alternatively, you may use any Rack handler by using a config.ru file:
# config.ru (it will run with rackup)
require './app_name'
run MyApp
Run:
rackup -p 4567
Using a config.ru with a Classic Style Application
Write your app file:
# file app.rb
require 'Sinatra'
get '/' do
'Hello everyone!'
end
And an associated config.ru:
require './app'
run Sinatra::Application
When should I use config.ru?
Having a config.ru file is advised if:
You want to deploy using an alternative Rack handler (such as Heroku, Passenger, or Unicorn).
You want to employ multiple Sinatra subclasses:
: Base Sinatra should not be used as an endpoint; rather, it should be used as middleware.
Simply changing to the modular style does not necessitate switching to a config.ru, and you are not required to run with the modular style when using a config.ru.
Middleware built with Sinatra
Any Sinatra application can be added in front of any Rack endpoint as middleware in addition to utilising other Rack middleware. Another Sinatra application or any other Rack-based application (Rails, Hanami, Roda, etc.) might be this endpoint
require 'Sinatra/base'
class LoginScreen < Sinatra::Base
enable :sessions
get('/login') { haml :login }
post('/login') do
if params['id_name'] == 'admin_user' && params['password'] == 'admin_password'
session['user_id_name'] = params['id_name']
else
redirect '/login'
end
end
end
class MyApp < Sinatra::Base
# # Middleware will be executed prior to filters.
use LoginScreen
before do
unless session['user_id_name']
halt "Access denied, please <a href='/login'>login</a>."
end
end
get('/') { "Hi #{session['user_id_name']}." }
end
Frequently Asked Questions
What exactly is the Sinatra test?
In public speaking, this is known as the Sinatra test. It entails gaining credibility in public speaking by using a difficult references. If you succeed in the most difficult circumstances, it can guarantee your future endeavours.
What exactly is Ruby in testing?
Ruby, like other programming languages, includes a framework called Test::Unit in its standard library for creating, organising, and running tests. There are other popular testing frameworks, such as RSpec and cucumber.
How does Sinatra function?
The web server responds to a series of queries that Sinatra sends and receives. These requests are contained in a controller file, which is part of your program and communicates with the internet. A controller is a component of the MVC architectural design pattern (Models-Views-Controllers).
What exactly is Rack middleware?
Rack middleware is a small component that aids in completing a task. Consider different middleware performing different tasks such as logging and caching. It can modify or halt requests before they reach the final middleware (The Sinatra assigned to run the method). Rn is Also known as MVC (Models-Views-Controllers).
Should I start by writing unit tests?
It is common practice to write the test first, followed by as much code as is required to pass it. It contributes to the practice of Test-Driven Development (TDD).
Bluefruit extensively uses TDD because it allows us to build the right product with minimal waste and redundancy.
Conclusion
In this blog, we have discussed Testing in Sinatra. We have seen differences between classic style and modular and how the application launches through modular serving. We have looked into using config.ru. And the building of middleware.
If you face any doubt, please comment, and we will love to answer your questions.
Want expertise in Ruby on rails for your next web development project? Check out our course.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!