sinatraでテストの入門の入門(その2)sinatraでrspec
以前書いたものの続きです。
題名の通りsinatraでrspecを使う基本的なことがらになります。
sinatraだけの設定を考えるとtest/unitと同じです。乱暴に言うとassertがshouldになるだけだと思ってよいと思います。
サンプルコード
前回のサンプルコードをrspecで書く。
require './hello_world' require 'rspec' require 'rack/test' set :environment, :test describe 'The HelloWorld App' do include Rack::Test::Methods def app Sinatra::Application end it "says hello" do get '/' last_response.should be_ok last_response.body.should eq 'Hello World' end it "says hello to a person" do get '/', :name => 'Simon' last_response.body.should include('Simon') end end
基本的にはunit/testと同じですね。
includeの共通化
test/unitと同様にRack::Test::Methodsのインクルードはhelperでやると
手間が省けます。
require 'rspec' require 'rack/test' Rspec.configure do |conf| conf.include Rack::Test::Methods end
こんな感じ。