Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Mocking Demystified               by @_md
how many of you  write tests?
how manywrite tests before the code?
how many of youknow what a mock is?
how many of you use mocks?
ALL YOU NEED TO KNOW ABOUT TESTING           (in 5 minutes)
testArrange          Act                 Assert
test        instantiateArrange        tested object       Act              Assert
instantiate          Arrange                  tested object$parser = new MarkdownParser;
testArrange                  run the method          Act     you want to test                 Assert
run the method          Act                  you want to test$parser = new MarkdownParser;$html = $parser->toHtml(Hello Wo...
testArrange          Act                 Assert specify the                        expected outcome
specify the          Assert                 expected outcome$parser = new MarkdownParser;$html = $parser->toHtml(Hello Wor...
testArrange         instantiate tested object          Act        run the method                Assert      check expected...
$this->toHtml(Hello World)     ->shouldReturn(<p>Hello World</p>);
$this->toHtml(Hello World)     ->shouldReturn(<p>Hello World</p>);     a test is an executable example      of a class exp...
READY TO MOCK AROUND?
why?
class ParserSubject{    public function notify(Event $event)    {        foreach ($this->subscribers as $subscriber) {    ...
class ParserSubjectTest extends TestCase{    /** @test */    function it_notifies_subscribers()    {         // arrange   ...
class EndOfListListener extends EventListener{    public function onNewLine(Event $event)    {        $html = $event->getT...
{                  focus on unit                  expensive to instantiate      why?        control on collaborators’ stat...
a mock is just 1 ofmany test double patterns
doubles are replacement of anything    that is not the tested object
{                       dummy                       fake             doubles   stub                       mock            ...
{                  dummy no behaviour                  fake                         control indirect output    doubles    ...
a mockist TDD, London school, approach:only the tested object is real
DOUBLES WITHOUT BEHAVIOUR
dummyhttp://wicker123.deviantart.com/art/Slappy-The-Dummy-148136425
testArrange          Act                 Assert
arrange          {          instantiate (may require collaborators)
class MarkdownParser{    private $eventDispatcher;    public function __construct(EventDispatcher $dispatcher)    {       ...
class MarkdownParser{    private $eventDispatcher;    public function __construct(EventDispatcher $dispatcher)    {       ...
class MarkdownParserTest extends TestCase{    function setUp()    {        $dispatcher = $this->getMock(EventDispatcher); ...
class MarkdownParser extends ObjectBehavior{    function let($dispatcher)    {        $dispatcher->beAMockOf(EventDispatch...
http://wicker123.deviantart.com/art/Slappy-The-Dummy-148136425      “Dummy is a placeholder passed to the       SUT (teste...
dummy: double with no behaviourhttp://wicker123.deviantart.com/art/Slappy-The-Dummy-148136425
DOUBLES TO CONTROL INDIRECT OUPUT
arrange          {          instantiate (may require collaborators)          further change state
class MarkdownParserTest extends TestCase{    function setUp()    {        $dispatcher = $this->getMock(EventDispatcher); ...
class MarkdownParserTest extends TestCase{    function setUp()    {        $dispatcher = $this->getMock(EventDispatcher); ...
arrange          {          instantiate (may require collaborators)          further change state          configure indir...
class EndOfListListener extends EventListener{    public function onNewLine(Event $event)    {        $html = $event->getT...
class EndOfListListener extends EventListener{    public function onNewLine(Event $event)    {        $html = $event->getT...
fakestub                                        doubles for controlling indirect output       http://www.flickr.com/photos/...
$event->getText(); // will return “Hello World”$this->document->getNextLine(); // will return “”                 STUBBING:...
function let($event, $document){    $event->beAMockOf("Event");    $document->beAMockOf("Document");    $this->beConstruct...
function let($event, $document){    $event->beAMockOf("Event");    $document->beAMockOf("Document");    $this->beConstruct...
function let($event, $document){    $event->beAMockOf("Event");    $document->beAMockOf("Document");    $this->beConstruct...
class EndOfListListener extends EventListener{    public function onNewLine(Event $event)    {        $html = $event->getT...
function let($event, $document){    $event->beAMockOf("Event");    $document->beAMockOf("Document");    $this->beConstruct...
double                       behaviourloose demand       returns null to any method callfake           returns null to def...
DOUBLES TO SPEC MESSAGE EXCHANGE
we said beforea test is an executable example of a class expected behaviour
what is behaviour?
B = I + O behaviour = input + output
what can happen in a method?
{          return a value          modify statemethods   print something          throw an exception          delegate
{          return a value                          not the final          modify state                            behaviou...
{methods          return a value we should probably          print something delegate that too          throw an exception...
{methods          return a value          throw an exception          delegate
method                strategyreturns a valuethrows an exceptiondelegates
method                     strategyreturns a value         assertions/throws an exception    expectationsdelegates        ...
Viewpoints Research Institute Source - Bonnie Macbird URL -http://www.vpri.org“The key in making great and growable system...
messaging
yeah, but what does it mean?
$this->person->getCar()->getEngine()->ignite();
Inappropriate Intimacy
$this->person->startCar();
tell, don’t ask![Sharp 1997]
exposing lower level implementation       makes the code rigid
$this->person->getCar()->getEngine()->ignite();
focus on messagingmakes the code flexible
$this->person->startCar();
mockspy                                              doubles for messaging               http://www.flickr.com/photos/scrib...
class ParserSubject{    public function notify(Event $event)    {        foreach ($this->subscribers as $subscriber) {    ...
class ParserSubject{    public function notify(Event $event)    {        foreach ($this->subscribers as $subscriber) {    ...
use mocks to describe how yourmethod will impact collaborators
use PHPUnit_Framework_TestCase as TestCase;class ParserSubjectTest extends TestCase{    /** @test */ function it_notifies_...
public function testUpdateWithEqualTypes(){    $installer = $this->createInstallerMock();    $manager   = new Installation...
github.com/padraicb/mockery
use PHPUnit_Framework_TestCase as TestCase;class ParserSubjectTest extends TestCase{    /** @test */ function it_notifies_...
class ParserSubject extends PHPSpec2ObjectBehavior{    /**      * @param Event $event      * @param Subscriber $subscriber...
use spies to describe indirect output   expectations, retrospectively
github.com/dancras/doubles
use PHPSpec2ObjectBehavior;class ParserSubject extends ObjectBehavior{    function it_notifies_subscribers()    {        $...
use PHPSpec2ObjectBehavior;class ParserSubject extends ObjectBehavior{    /**      * @param Event $event      * @param Sub...
use ProphecyProphet;class TestCase extends SomeUnitTestingFrameworkTestCase{    function createDoubler($name)    {        ...
{                  dummy no behaviour                  fake                        control indirect output    doubles     ...
Marcello Duarte  I work hereI contribute here  I tweet here @_md
Thank you !
Questions or Comments?want to improve on testing? bit.ly/inviqa-bdd-training
function let($event, $document){ $event->beAMockOf("Event"); $document->beAMockOf("Document");
Upcoming SlideShare
Loading in …5
×

function let($event, $document){ $event->beAMockOf("Event"); $document->beAMockOf("Document"); Mocking Demystified

17,032 views

Published on

function let($event, $document){ $event->beAMockOf("Event"); $document->beAMockOf("Document"); $this->beConstructedWith($document);}function it_ends_list_when_next_is_empty($event, $document){ $event->getText()->willReturn("Some text"); $document->getNextLine()->willReturn(""); $this->onNewLine($event) ->shouldReturn("Some text</li></ul>");} THE SPEC AGAIN

Published in: Technology

×