6. London school TDD
ロンドンのXPコミュニティが発祥
モックを積極的に使い、状態ではなく振る舞いに注目したTDDの
手法
Growing Object-Oriented Software Guided By Tests(2009)
通称The GOOS Book
実践テスト駆動開発(2012)
phpspecの作者「London school TDDを学ぶ上でベストなリソース」
(Full Stack Radio #15)
ただし結構難しい本
◦ GUIあり、ネットワーク通信あり、マルチスレッドあり
◦ 難しい問題を解決する手法を解説しようとすると、例も難しくなると
いう技術書のジレンマ
7. 個人的な体験として
The GOOS Bookを読む → 「なるほど(わからん)」
phpspecを知る → 「なるほど(わからん)」
phpspecについて調べる → 「それThe GOOS Bookに書いてあるよ」
The GOOS Bookを読む → 「ホンマや!」
💡 phpspecを学ぶこ
とでLondon School
TDDが理解できた
(気がする)
phpspecを使えば
London School TDD
を上手く説明でき
るのでは?
10. Outside-In/Inside-Out
class A {
function __construct(B $b) {
}
}
class B {
function __construct(C $c, D $d) {
}
}
class C {
}
class D {
}
Outside-In
◦ Bをダミー(Mock, Stub)に置き換え
てAをテストする
◦ AがBに要求しているものが明確にな
る
Inside-Out
◦ C,Dを先に実装してからBを実装する
◦ テストの粒度は次第に大きくなる
◦ ダミーを実装する手間はかからない
◦ Bを実装中はAが存在しないので、A
がBに何を要求するかわからない
29. class MarkdownSpec extends ObjectBehavior
{
...
function it_convers_text_from_an_external_source()
{
// $reader->getMarkdown()が"Hi, there"を返すとする
$this->toHtmlFromReader($reader)
->shouldReturn("<p>Hi, there</p>");
}
spec/MarkdownSpec.php
30. class MarkdownSpec extends ObjectBehavior
{
...
function it_convers_text_from_an_external_source()
{
$reader->getMarkdown()->willReturn("Hi, there");
$this->toHtmlFromReader($reader)
->shouldReturn("<p>Hi, there</p>");
}
spec/MarkdownSpec.php
31. use Reader;
class MarkdownSpec extends ObjectBehavior
{
...
function it_convers_text_from_an_external_source(Reader
$reader)
{
$reader->getMarkdown()->willReturn("Hi, there");
$this->toHtmlFromReader($reader)
->shouldReturn("<p>Hi, there</p>");
}
spec/MarkdownSpec.php
exampleに仮引数を書くとPhpSpecが$readerを渡
してくれる
32. C:¥Users¥ishida¥src¥phpspec>vendor¥bin¥phpspec run --fake
...
Would you like me to generate an interface `Reader` for you?
[Y/n]
...
Would you like me to generate a method signature
`Reader::getMarkdown()` for you?
[Y/n]
...
Do you want me to create `Markdown::toHtmlFromReader()` for you?
[Y/n]
...
Do you want me to make `Markdown::toHtmlFromReader()` always
return '<p>Hi, there</p>' for you?
[Y/n]
Method Markdown::toHtmlFromReader() has been modified.
/ skipped: 0% / pending: 0% / passed: 100% / failed: 0% / broken: 0%
/ 3 examples
1 specs
3 examples (3 passed)
43ms
Reader::getMarkdown()と
Makkdown::toHtmlFromReader()の仮実装の
自動生成
49. 参考資料
phpspec2: SUS and collaborators(2012)
(http://everzet.com/post/33178339051/sus-collaborators)
phpspec2のalphaリリース時の作者によるコンセプトの解説
Konstantin Kudryashov - Design How Your Objects Talk Through Mocking
at Laracon EU 2014
(https://www.youtube.com/watch?v=X6y-OyMPqfw)
Laracon EUでの作者によるモックを使った開発手法に関する講演。
phpspec自体は使われていない
Does TDD really lead to good design?
(http://codurance.com/2015/05/12/does-tdd-lead-to-good-design/)
Detroit schoolとLondon Schoolの比較など
Laracast
(https://laracasts.com/index/phpspec)
LaravelやPHPに関するスクリーンキャスト。一部無料