sinatraでテストの入門の入門(その2)sinatraでrspec

以前書いたものの続きです。
題名の通りsinatrarspecを使う基本的なことがらになります。
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

こんな感じ。

まとめ

1. rspec on sinatraも簡単
2. rspec固有の問題がないので、大半がサンプルコードという
中身が薄い日記になった。

こんな感じです。