LoginSignup
36
35

More than 5 years have passed since last update.

Markdown to RSpec

Last updated at Posted at 2013-07-03

RSpecはRubyのテストフレームワークです。TDDやBDDに利用されています。
ステークフォルダーの価値(振る舞い)を自然言語に近い形でテストコードに記述することができます。

Usage

テストコードは振る舞いを3つのメソッドで記述します。

method examplar
describe 概要、機能
context 行動、状態
example 期待

describecontextは任意で階層を増やすことができます。
(contextdescribeのエイリアスです。用途に合わせて使います。)

example_spec.rb
describe "概要" do
  describe "機能" do
    context "行動" do
      context "状態" do
        example "期待"
      end
    end
  end
end

xxx_spec.rbファイルに振る舞いを記述するので、設計のドキュメンテーションを
Markdownで記述するとRSpecにコンバートが簡単です。

Example

Markdown

メモ帳のアプリケーションを簡単に設計してみます。

notepad_spec.md
# メモ帳

## 新規
* メモ帳が作成される
* タイトルは無題で表示される
* テキストは空白で表示される

## 開く
* 選択するファイルの一覧が表示される
* 選択したファイル名がタイトルに表示される
* 選択したファイルの内容がテキストに表示される

## 保存
### メモ帳が新規の場合
* ファイル名を入力して保存する

### メモ帳が更新の場合
* ファイルを上書きで保存する

RSpec

このMarkdownをRSpecに置換えます。

notepad_spec.rb
describe "メモ帳" do
  describe "新規" do
    example "メモ帳が作成される"
    example "タイトルは無題で表示される"
    example "テキストは空白で表示される"
  end

  describe "開く" do
    example "選択するファイルの一覧が表示される"
    example "選択したファイル名がタイトルに表示される"
    example "選択したファイルの内容がテキストに表示される"
  end

  describe "保存" do
    context "メモ帳が新規の場合" do
      example "ファイル名を入力して保存する"
    end

    context "メモ帳が更新の場合" do
      example "ファイルを上書きで保存する"
    end
  end
end

Pending

まずは example にブロックの記述がないのでPENDINGになります。
(rspec -cf dコマンドでスペックがカラーで階層に表示されます。)

$ rspec -cf d notepad_spec.rb 

メモ帳
  新規
    メモ帳が作成される (PENDING: Not yet implemented)
    タイトルは無題で表示される (PENDING: Not yet implemented)
    テキストは空白で表示される (PENDING: Not yet implemented)
  開く
    選択するファイルの一覧が表示される (PENDING: Not yet implemented)
    選択したファイル名がタイトルに表示される (PENDING: Not yet implemented)
    選択したファイルの内容がテキストに表示される (PENDING: Not yet implemented)
  保存
    メモ帳が新規の場合
      ファイル名を入力して保存する (PENDING: Not yet implemented)
    メモ帳が更新の場合
      ファイルを上書きで保存する (PENDING: Not yet implemented)

Implement

Notepadクラスにcreateメソッドを実装したとします。
(この例はスタブで必ずテストが成功するようにしています。)

notepad_spec.rb
describe "Notepad" do
  describe ".create" do
    let(:notepad) {Object.new}

    example "creates a notepad" do
      notepad.stub(:title) {"無題"}
      expect(notepad.title).to eq("無題")
    end

    example "returns blank to the text" do
      notepad.stub(:text) {nil}
      expect(notepad.text).to be_nil
    end
  end
end

クラスメソッドは.、インスタンスメソッドに#describeに記述すると出力が読みやすくなります。

$ rspec -cf d notepad_spec.rb 

Notepad
  .create
    creates a notepad
    returns blank to the text

他にもbeforeletなどの便利なメソッドがRSpecにあります。

要件から設計(タスク化)をするときに受入(テスト)まで考慮すると
開発でのソースコードが仕様書に近くなるので素敵です。

Tips

RSpecのアサーションの推奨がshouldからexpectに変更されました。

should

冗長かと思いながらもitshouldから始めていました。

it "should return blank to the text" do
  notepad.text.should be_nil
end

expect

expectに変えるついでにitshouldも外しました。
(ただitは三人称単数なので動詞の最後にsを付ける必要があります。)

it "returns blank to the text" do
  expect(notepad.text).to be_nil
end

example

itは三人称単数を気にすることになるのでexampleに変更しました。

example "returns blank to the text" do
  expect(notepad.text).to be_nil
end
36
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
35