RubyといえばRuby on Railsが有名ですが、DBを使わないシンプルなアプリケーションの場合はSinatraで十分な気がします。
この記事では、SinatraのインストールとRspecでテストする方法を紹介します。
SinatraのInstall
sinatra_sampleというディレクトリを作成してそこにアプリを作成します。
1
2
mkdir sinatra_sample
cd sinatra_sample
Gemfileを作成します。
1
2
3
4
5
# Gemfile
source 'https://rubygems.org'
gem 'rake'
gem 'sinatra'
bundle install でインストールします。
アプリケーションを作成
myapp.rbを作成します。
1
2
3
4
5
6
# my_app.rb
require 'sinatra'
get '/' do
'Hello world!'
end
以下のコマンドで実行します。
ブラウザで以下にアクセスするとHello world!と表示されます。
http://localhost:4567
あっという間にできましたね。
Rspecでテストをする
RspecでテストするためにRspecをインストールします。
1
2
3
4
5
6
7
8
9
10
11
# Gemfile
source 'https://rubygems.org'
gem 'rake'
gem 'sinatra'
group :test do
gem 'rspec'
gem 'rack-test'
end
bundle installでインストールします。
spec/spec_helper.rb を作成します。
spec_helper.rbはrspecの設定を行うためのファイルです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# spec/spec_helper.rb
require 'rack/test'
require 'rspec'
ENV [ 'RACK_ENV' ] = 'test'
require File . expand_path '../../myapp.rb' , __FILE__
module RSpecMixin
include Rack :: Test :: Methods
def app () Sinatra :: Application end
end
RSpec . configure { | c | c . include RSpecMixin }
spec/myapp_spec.rbを作成します。
myapp_spec.rbはアプリケーションをテストするためのファイルです。
1
2
3
4
5
6
7
8
9
# spec/app_spec.rb
require File . expand_path '../spec_helper.rb' , __FILE__
describe "My Sinatra Application" do
it "should allow accessing the home page" do
get '/'
expect ( last_response ) . to be_ok
end
end
テストを実行してみましょう。
1
2
3
4
5
⇒ bundle exec rspec spec
.
Finished in 0 . 0311 9 seconds ( files took 0 . 20975 seconds to load )
1 example , 0 failures
テストが成功しました!
参考文献
資料は以下を参照しました。
[英語]http://www.sinatrarb.com/intro.html
[日本語]http://www.sinatrarb.com/intro-ja.html
[Sinatra Recipes]http://recipes.sinatrarb.com/