let's begin behavior driven development using rspec

Post on 19-Jan-2015

1.974 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Introducing BDD and RSpec.

TRANSCRIPT

http://www.flickr.com/photos/bruce_mcadam/3214482163/

Let’s begin Behavior Driven Developmentusing RSpec村田賢太 mrkn

CLR/H #582011.05.21

2011年5月22日日曜日

自己紹介

2011年5月22日日曜日

http://www.flickr.com/photos/koichiroo/5244581973/

村田賢太mrkn

高専カンファレンス副代表

Ruby コミッタRuby 札幌

2011年5月22日日曜日

2010年3月1日月曜日2011年5月22日日曜日

http://www.flickr.com/photos/bruce_mcadam/3214482163/2011年5月22日日曜日

http://www.flickr.com/photos/bruce_mcadam/3214482163/

Let’s begin Behavior Driven Developmentusing RSpec村田賢太 mrkn

CLR/H #582011.05.21

2011年5月22日日曜日

三部構成

✓知識編

✓準備編

✓実践編

2011年5月22日日曜日

http://www.flickr.com/photos/bruce_mcadam/3214482163/

知識編2011年5月22日日曜日

BDDBehavior Driven Development

2011年5月22日日曜日

TDDTest Driven Development

2011年5月22日日曜日

TDDTest Driven Development

2011年5月22日日曜日

TDD → BDDという変化

2011年5月22日日曜日

なぜ?

2011年5月22日日曜日

× 機能のテスト

〇 振る舞いの設計

2011年5月22日日曜日

何が違う?

2011年5月22日日曜日

言葉遣い態 度

2011年5月22日日曜日

test-unit

2011年5月22日日曜日

module System class AgeCalculatorTest < Test::Unit::TestCase include RR::Adapters::TestUnit

def setup @calc = AgeCalculator.with_birthday(1981, 7, 20) end

def test_age_on assert_equal(29, @calc.age_on(2011, 7, 19)) assert_equal(30, @calc.age_on(2011, 7, 20)) end

def test_age_today d, m, y = [*Time.now][3..5] stub(@calc).age_on @calc.age_on_today assert_received(@calc) {|c| c.age_on(y, m, d) } end endend

2011年5月22日日曜日

RSpec

2011年5月22日日曜日

module System describe AgeCalculator do context ‘with birthday 1981-07-20’ do subject { AgeCalculator.with_birthday(1981, 7, 20) }

describe ‘#age_on’ do context ‘with 2011-07-19’ do specify ‘the age is 29’ do subject.age_on(2011, 7, 19).should be == 29 end end

context ‘with 2011-07-20’ do specify ‘the age is 30’ do subject.age_on(2011, 7, 20).should be == 30 end end end

describe ‘#age_on_today’ do it ‘calls age_on with year, month, and day on today’ do d, m, y = [*Time.now][3..5] subject.should_receive(:age_on).with(y, m, d) subject.age_on_today end end end endend

2011年5月22日日曜日

見た目は違うけどやってる事は同じ

2011年5月22日日曜日

見た目も違うし、やってる事も同じに見えるけど違う

2011年5月22日日曜日

言葉遣い態 度

2011年5月22日日曜日

そもそもTDDと言いながら我々は本当にテストをしていると言えるのだろうか?

2011年5月22日日曜日

http://www.ne.jp/asahi/t/wada/articles/Test_in_TDD.pdf

!Test"#$%&'()$*

t-wadahttp://d.hatena.ne.jp/t-wada/

2005+7,23-@J2EE./012103

2011年5月22日日曜日

http://www.ne.jp/asahi/t/wada/articles/Test_in_TDD.pdf

!"#$%1

! TDD&'()*+,&-.! "Test"/01213.45%&6784

" 9:;<=>?@A

" 2B%"Test"CDE&-.FGHIJKLF" MNO5PQCRS

! '()TUVWXOYZ[4

" 9:\#/ ]^#/ >?@A_`\

2011年5月22日日曜日

http://www.ne.jp/asahi/t/wada/articles/Test_in_TDD.pdf

!"#$%1

! TDD&'()*+,&-.! "Test"/01213.45%&6784

" 9:;<=>?@A

" 2B%"Test"CDE&-.FGHIJKLF" MNO5PQCRS

! '()TUVWXOYZ[4

" 9:\#/ ]^#/ >?@A_`\

2011年5月22日日曜日

http://www.ne.jp/asahi/t/wada/articles/Test_in_TDD.pdf

!"#$%2

! TDD%"Test"&'(())%*+%,%" -./0'(12%345

! TDD%67&89:;<=/>?@ABCD:;EFG>/HIJKL

2011年5月22日日曜日

http://www.ne.jp/asahi/t/wada/articles/Test_in_TDD.pdf

!"#$%2

! TDD%"Test"&'(())%*+%,%" -./0'(12%345

! TDD%67&89:;<=/>?@ABCD:;EFG>/HIJKL

2011年5月22日日曜日

http://blogs.itmedia.co.jp/hiranabe/2005/08/sd4__c05e.html2011年5月22日日曜日

http://blogs.itmedia.co.jp/hiranabe/2005/10/tdd__bdd__731d.html2011年5月22日日曜日

× 機能のテスト

〇 振る舞いの設計

2011年5月22日日曜日

テストを書いてからコードを書く

振る舞いを設計してからコードを書く

2011年5月22日日曜日

module System describe AgeCalculator do context ‘with birthday 1981-07-20’ do subject { AgeCalculator.with_birthday(1981, 7, 20) }

describe ‘#age_on’ do context ‘with 2011-07-19’ do specify ‘the age is 29’ do subject.age_on(2011, 7, 19).should be == 29 end end

context ‘with 2011-07-20’ do specify ‘the age is 30’ do subject.age_on(2011, 7, 20).should be == 30 end end end

describe ‘#age_on_today’ do it ‘calls age_on with year, month, and day on today’ do d, m, y = [*Time.now][3..5] subject.should_receive(:age_on).with(y, m, d) subject.age_on_today end end end endend 動く設計書

2011年5月22日日曜日

知識編まとめ

✓TDDと言ってもテストしてるわけじゃない

✓“テストを書く” → “振る舞いを設計する”

✓設計の成果物が動くテスト

2011年5月22日日曜日

http://www.flickr.com/photos/bruce_mcadam/3214482163/

準備編2011年5月22日日曜日

インストール

2011年5月22日日曜日

Ruby 1.9.2p180

RSpec 2.5.0

2011年5月22日日曜日

Ruby 1.9.2p180

RSpec 2.5.0

2011年5月22日日曜日

RVMRuby Version Manager

2011年5月22日日曜日

http://gihyo.jp/dev/serial/01/ruby/00392011年5月22日日曜日

http://www.nicovideo.jp/watch/sm129758492011年5月22日日曜日

インストールは出来たことにしよう

2011年5月22日日曜日

Ruby 1.9.2p180

RSpec 2.5.0

2011年5月22日日曜日

gem install rspec

2011年5月22日日曜日

rspec の依存関係rspec-core

rspec-expectationsrspec-mocks

2011年5月22日日曜日

ほかにあると便利なもの

✓autotest

✓bundler

✓simplecov

2011年5月22日日曜日

準備編まとめ

✓RVM を使って Ruby をインストールした

✓gem install rspec で RSpec 一式をインストールした

2011年5月22日日曜日

http://www.flickr.com/photos/bruce_mcadam/3214482163/

実践編2011年5月22日日曜日

RSpec を使い始める

2011年5月22日日曜日

いくつかの状況

1.TDDをやっている途中

2.コードはあるけどテストはない

3.ゼロから始める

2011年5月22日日曜日

(1)

2011年5月22日日曜日

TDDをやっている途中

✓test-unit から RSpec へ移行

✓テストを振る舞いの記述に書き換える

✓漏れなく書き換えられるか不安

2011年5月22日日曜日

RSpec 1 の場合

✓require ‘spec/test/unit’ を使う

✓spec コマンドで test-unit の TestCase を実行できる

✓書き換える必要が無い

2011年5月22日日曜日

RSpec 2 の場合

✓‘spec/test/unit’ 相当が無い

✓自分で少しずつ書き換えが必要

✓漏れなく書き換えられるか不安

2011年5月22日日曜日

そこでカバレッジ

http://www.slideshare.net/t_wada/sapporo-rubykaigi01-twada-lt-presentation2011年5月22日日曜日

そろそろカバレッジについて一言いっておくか

和田 卓人 (a.k.a id:t-wada)

Oct, 26, 2008 @SapporoRubyKaigi 01

http://www.slideshare.net/t_wada/sapporo-rubykaigi01-twada-lt-presentation2011年5月22日日曜日

カバレッジを監視することで安心を得る

2011年5月22日日曜日

RSpec 1 を使って書き換えてから RSpec 2 に移行しても良い

2011年5月22日日曜日

(2)

2011年5月22日日曜日

コードはあるけどテストはない

2011年5月22日日曜日

遺産(Legacy)

Photo by rla579: http://flickr.com/photos/rla579/2482286520/2011年5月22日日曜日

いきいきとしたレガシーコードとの暮らし

札幌Ruby会議01;北海道情報大学札幌サテライト; 2008-10-25(土)

角谷 信太郎Working Effectively with Legacy tDiary Codeusing Cucumber and RSpec

日本Rubyの会(株)永和システムマネジメント s-kakutani@esm.co.jp

KAKUTANI Shintaro; Eiwa System Management,Inc.; Nihon Ruby-no-kai

http://kakutani.com/articles/working_effectively_with_legacy_tdiary_code_using_cucumber_and_rspec.pdf2011年5月22日日曜日

(3)

2011年5月22日日曜日

ゼロから始める

2011年5月22日日曜日

BDDでゼロから何かを作ってみよう

2011年5月22日日曜日

FizzBuzzCounter

2011年5月22日日曜日

FizzBuzz✓1から順に数を数える

✓3の倍数の時 “Fizz”

✓5の倍数の時 “Buzz”

✓3と5の公倍数の時 “FizzBuzz”

2011年5月22日日曜日

# たとえばこんな感じで動くものFizzBuzzCounter.new.each do |i| p iend#=> 1# 2# “Fizz”# 4# “Buzz”

2011年5月22日日曜日

# こういう使い方もできたらいいfbc = FizzBuzzCounter.newp fbc.next #=> 1p fbc.next #=> 2p fbc.next #=> “Fizz”fbc.each {|i| break if i > 14 }p fbc.next #=> “FizzBuzz”

2011年5月22日日曜日

# Enumerable としても使いたいfbc = FizzBuzzCounter.newp fbc.take(3) #=> [1, 2, “Fizz”]p fbc.take(3) #=> [4, “Buzz”, “Fizz”]

2011年5月22日日曜日

開発の流れ1.最初 spec ファイル [Failed]

2.空のクラスを作る [Success]

3.振る舞いを一つ記述する [Failed]

4.それを実装する [Success]

5.3 に戻る

2011年5月22日日曜日

ディレクトリ構造と設定✓実装ファイルは lib ディレクトリの中に置く

✓spec ファイルは spec ディレクトリの中に lib ディレクトリ内と同じ階層構造で置く

✓$LOAD_PATH などの共通設定は spec/spec_helper.rb に書き、各 spec ファイル で require ‘spec_helper’ する。

2011年5月22日日曜日

Live Coding

2011年5月22日日曜日

http://jp.rubyist.net/magazine/?0032-TranslationArticle

RSpec Best Practice2011年5月22日日曜日

describe FizzBuzzCounter do describe ‘.filter’ do context ‘with 1’ do it ‘returns 1’ do FizzBuzzCounter. filter(1).should be == 1 end end endend

2011年5月22日日曜日

describe FizzBuzzCounter do describe ‘.filter’ do context ‘with 1’ do it ‘returns 1’ do FizzBuzzCounter. filter(1).should be == 1 end end endend

should は省略する

クラスメソッドにはピリオド

メソッドの実引数を context で説明する

2011年5月22日日曜日

Observer Pattern

2011年5月22日日曜日

+ attach_observer(observer)+ detach_observer(observer)+ notify()

Observable+ update()Observer

subject

observers

1 *

ConcreteObserverClient+ change()ConcreteObserver

2011年5月22日日曜日

aConcreteObservable aConcreteObserver anotherConcreteObserveraClient

notify()

update()

update()

change()

2011年5月22日日曜日

複数のオブジェクト間における相互作用を設計する

2011年5月22日日曜日

aConcreteObservable aConcreteObserver anotherConcreteObserveraClient

notify()

update()

update()

change()

2011年5月22日日曜日

振る舞いの抽出

✓対象の change() を呼び出したら、それ自身の notify() が呼び出される

✓対象の change() を呼び出したら、対象に事前に登録してあるオブザーバの update() が呼び出される

2011年5月22日日曜日

Test Double

2011年5月22日日曜日

describe ConcreteObservable do describe ‘#change’ do it ‘calls its notify()’ do subject.should_receive(:notify) subject.change end endend

2011年5月22日日曜日

describe ConcreteObservable do describe ‘#change’ do subject { ConcreteObservable.new }

it “calls observers’ update()” do observer = double(‘an observer’) observer.should_receive(:update) subject.change end endend

2011年5月22日日曜日

http://ironruby.codeplex.com/2011年5月22日日曜日

top related