42. $ phpspec StringCalculatorSpec.php -c
..*
Pending:
String Calculator returns the bare number
# Waiting to clarify the spec
# ./spec/StringCalculatorSpec.php:19
Finished in 0.056134 seconds
2 examples, 0 failures, 1 pending
$
43. Failing examples
public function itReturnsTheSumOfSpaceSeparatedString()
{
$this->calculator->add('4 2')->should->be(6);
}
// this will fail because it’s not yet implemented
44. $ phpspec StringCalculatorSpec.php -c
.*.F
Pending:
String Calculator returns the bare number
# Waiting to clarify the spec
# ./spec/StringCalculatorSpec.php:19
Failures:
1) String Calculator returns the sum of space separate string
expected 6, got NULL (using be())
# .spec/StringCalculatorSpec.php:28
Finished in 0.056134 seconds
4 examples, 1 failure, 1 pending
$
46. $ phpspec StringCalculatorSpec.php -c
.*.F
Pending:
String Calculator returns the bare number
# Waiting to clarify the spec
# ./spec/StringCalculatorSpec.php:19
Failures:
1) String Calculator returns the sum of space separate string
expected 42, not to be 24 (using be())
# .spec/StringCalculatorSpec.php:28
Finished in 0.056134 seconds
4 examples, 1 failure, 1 pending
$
48. $ phpspec StringCalculatorSpec.php -c
.*.F
Pending:
String Calculator returns the bare number
# Waiting to clarify the spec
# ./spec/StringCalculatorSpec.php:19
Failures:
1) String Calculator returns the sum of space separate string
expected 42, got 0 (using be())
# .spec/StringCalculatorSpec.php:28
2) StringCalculator returns the sum of any white space
separated string
Failure/Error:
Just because
Finished in 0.056134 seconds
4 examples, 1 failure, 1 pending
53. beInteger()
beLessThan($match)
beLessThanOrEqualTo($match)
beNull()
beString()
beTrue()
w
throwException($match) ne
54. Predicate Matcher
$cell = $this->spec(new Cell);
$cell->should->beAlive();
class Cell
{
protected $alive = true;
public function isAlive() {
return $this->alive;
}
...
}
55. Predicate Matcher
$newNode = $this->spec(new Node);
$newNode->shouldNot->haveChildren();
class Node
{
protected $children = array();
public function hasChildren() {
return count($this->children) > 0;
}
...
}
56. User De ned Matcher
class DescribeRapture extends PHPSpecContext
{
...
function itIsNotTheEndOfTheWorldAsWeKnowIt()
{
$today = new DateTime('2011-05-20');
$this->rapture->setDate($today)
$this->rapture->shouldNot->happenToday();
}
}
57. $ phpspec RaptureSpec.php -c
E
Exceptions:
1) Rapture is not the end of the world as we know it
Failure/Error: $this->rapture->shouldNot->happenToday();
PHPSpecException: unknown method call
# ./RaptureSpec.php:19
# /usr/share/pear/PHPSpec/Runner/Example.php:168
Finished in 0.056134 seconds
1 examples, 1 exception
$
58. PHPSpecMatcherdefine('happenToday', function() {
return array (
'match' => function($rapture) {
return $rapture->happensToday();
},
'failure_message_for_should_not' =>
function($rapture) {
return sprintf("expected %s not to mean kaput",
$rapture->getDate());
}
);
});
class DescribeRapture extends PHPSpecContext
{ ...
function itIsNotTheEndOfTheWorldAsWeKnowIt() {
$today = new DateTime('2011-05-20');
$this->rapture->setDate($today)
$this->rapture->shouldNot->happenToday();
}
}
60. PHPSpecMatcherdefine('contain', function($course) {
return array (
'match' => function($repository) use ($course) {
return $repository->has($course);
},
'failure_message_for_should' =>
function($repository) use ($course) {
return "expected $course to be in repository";
}
);
});
class DescribeCourseRepository extends PHPSpecContext
{ ...
function itAddsNewCourses() {
$course = new Course("BDD with PHPSpec");
$this->courses->add($course);
$this->courses->should->contain(course);
}
}
61. $ phpspec CourseRepositorySpec.php -c
F
Failures:
1) Course Repository adds new courses
Failure/Error: $this->courses->should->contain(course);
expected 'BDD with PHPSpec' to be in repository
# ./CourseRepository.php:19
Finished in 0.056134 seconds
1 examples, 1 failure
$
68. Mocks and hinted type
class HelloWorld
{
public function __construct(Greeter $greeter) // <-- type
{
...
}
}
$greeter = mock('Greeter'); // <-- we are covered
69. Empty doubles
class HelloWorld
{
public function __construct($greeter) // <-- no type
{
...
}
}
$greeter = mock(); // <-- no need for types