BT

最新技術を追い求めるデベロッパのための情報コミュニティ

寄稿

Topics

地域を選ぶ

InfoQ ホームページ ニュース RSpecに待ちに待ったIntegration TestのRBehaveが追加

RSpecに待ちに待ったIntegration TestのRBehaveが追加

ブックマーク
RSpec(サイト・英語)はRuby用に書かれた振舞駆動開発(略してBDD(サイト・英語))の受け入れテスト用フレームワークである。またRSpecはJRuby環境下で動作可能なので、JavaでもRSpecを使う事が出来る。歴史的にRSpecはストーリーレベルのIntegration testを実行するにはアドオンの拡張機能を必要とした。しかしDavid Chelimsky氏はRBehaveの機能を統合するためにRSpecにPlainTextStoryRunnerを直接追加した。

RSpecが提供する仕組みによって、開発者は機能から受入仕様を理解する事が出来たり、それらの仕様を読む事が出来て尚かつ実行可能な実例に変換することが出来る。そしてそれらの例は、ドキュメント、テスト、機能を把握するのが容易なビルドリポートとして用いることが可能である。RSpecは単体レベルのテストでは実用的だが、ストーリレベルのIntegration testを行うには難しい設定が必要だった。Dan North氏は別個の拡張機能であるRBehave(サイト・英語)を作った。そしてストーリレベルの振る舞いをGiven、With、Thenという形をとる一連のステップで表した(North氏が最初このパターンを記述したのは、JBehave(サイト・英語)のコンテキストにストーリの要件を記録するのが目的だった)。

David Chelimsky氏は、自身のブログ(source)の記事に書いてある通り、RSpecのtrunkにPlainTextStoryRunnerをコミットしたそうである。そのランナーによってRSpecにRBehaveの機能が追加される。

North氏が書いた典型的なRBehaveのサンプルは以下。

require ‘rubygems’
require ‘rbehave’
# shouldメソッドを使うためにspecをrequireします。
# 実際テストするアプリケーションコードをrequireします。

Story "transfer to cash account",
%(As a savings account holder
  I want to transfer money from my savings account
  So that I can get cash easily from an ATM) do

  Scenario "savings account is in credit" do
    Given "my savings account balance is", 100 do |balance|
      @savings_account = Account.new(balance)
    end
    Given "my cash account balance is", 10 do |balance|
      @cash_account = Account.new(balance)
    end
    When "I transfer", 20 do |amount|
      @savings_account.transfer_to(@cash_account, amount)
    end
    Then "my savings account balance should be", 80 do |expected_amount|
      @savings_account.balance.should == expected_amount
    end
    Then "my cash account balance should be", 30 do |expected_amount|
      @cash_account.balance.should == expected_amount
    end
  end

  Scenario "savings account is overdrawn" do
    Given "my savings account balance is", -20
    Given "my cash account balance is", 10
    When "I transfer", 20
    Then "my savings account balance should be", -20
    Then "my cash account balance should be", 10
  end
end
新しいRSpecでは、上記のコードは有効なステップを定義する以下のようなRubyファイルになります。 
class AccountSteps < Spec::Story::StepGroup
  steps do |define|
    define.given("my savings account balance is $balance") do |balance|
      @savings_account = Account.new(balance.to_f)
    end

    define.given("my cash account balance is $balance" do |balance|
      @cash_account = Account.new(balance.to_f)
    end

    define.then("my savings account balance should be $expected_amount" do |expected_amount|
      @savings_account.balance.should == expected_amount.to_f
    end

    define.then("my cash account balance should be $expected_amount" do |expected_amount|
      @cash_account.balance.should == expected_amount.to_f
    end
  end
end

steps = AccountSteps.new do |define|
  define.when("I transfer $amount") do |amount|
    @savings_account.transfer_to(@cash_account, amount.to_f)
  end
end
上記のステップに関するストーリの振る舞いを定義したプレーンなテキストファイルは以下。
Story: transfer to cash account
  As a savings account holder
  I want to transfer money from my savings account
  So that I can get cash easily from an ATM

  Scenario: savings account is in credit
    Given my savings account balance is 100
    And my cash account balance is 10
    When I transfer 20
    Then my savings account balance should be 80
    And my cash account balance should be 30

  Scenario: savings account is overdrawn
    Given my savings account balance is -20
    And my cash account balance is 10
    When I transfer 20
    Then my savings account balance should be -20
    And my cash account balance should be 10 
(訳:
ストーリ:預金勘定への移動
普通預金口座所持者として私は普通預金口座からお金を移動したいです
そうすることによって、ATMから簡単にお金を引き出す事が出来ます。

シナリオ:普通預金口座には残高があります。
私の普通預金口座の残高は100です。
そして私の現金勘定の残高は10です。
 20を移動したら私の普通預金口座の残高は80であるべきです。
そして現金勘定の残高は30であるべきです。

シナリオ:普通預金口座が借り越される
私の普通預金口座の残高が、-20だとします。
そして私の現金勘定の残高が10だとします。
20移動したら私の普通預金口座の残高は、-20であるべきです。
そして私の現金勘定の残高は10であるべきです。)

上で紹介したステップとストーリの振る舞いを定義したプレーンなテキストファイルをくっ付けて実行するRubyファイル

require 'spec'
require 'path/to/your/library/files'
require 'path/to/file/that/defines/account_steps.rb'
# 他のストーリを記述したファイルは、このファイルから.rbという拡張子を取り除いたものと同じように命名されるとみなします。
runner = Spec::Story::Runner::PlainTextStoryRunner.new(File.expand_path(__FILE__).gsub(".rb",""))
runner.steps << AccountSteps.new
runner.run

プレーンなテキストファイルの数ステップの言い回しがStepGroupに定義されたステップに一致しないといけない。そしてStepGroupに定義されたステップは非常に沢山あるので扱いにくいかもしれない。それらをより簡単にするために、Aslak Hellesøyはブラウザベースのエディタ開発に取り組んでいる(source)。そのエディタには、ステップの自動補完機能と配置済みパラメータの編集機能が付いている。

原文はこちらです:http://www.infoq.com/news/2007/10/RSpec-incorporates-RBehave

この記事に星をつける

おすすめ度
スタイル

BT