Mock Mock Mock - Mocking-Frameworks

Mock Mock Mock - Mocking-Frameworks

Mocks in unit tests. You can't live with them. You can't live without them. You need them. But as you plan on how to test your code, you also should ask yourself which Mocking-Library do you want to use. New project, legacy code or code that is hard to test: depending on the project, some libraries can be really helpful to build up your tests and save time. This talk is about common Mocking-Libraries and their (dis)advantages.
The talk of those slides were part of PHPUGKA Oct. 22nd. 2015.

A45e9d9d06ac3d88dcb4f2b9606d58c2?s=128

Claudio Zizza

October 22, 2015
Tweet

Transcript

  1. Mock Mock Mock Mock Mock Mock Mocking-Frameworks Mocking-Frameworks Claudio Zizza

    @SenseException
  2. Test your code! (Duh!)

  3. Tests & Dependencies

  4. Dependencies I only want to test/run this small part :-(

  5. Test Doubles Mostly used in tests: • Dummies • Stubs

    • Mocks (We do only Mocks for now)
  6. PHPUnit $mockFw = new PHPUnit_Framework_MockObject_Generator(); $math = $mockFw->getMock(MathInterface::class); $math->expects(new PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce())

    ->method('sum') ->with(1, 1) ->willReturn(2);
  7. Prophecy $mockFw = new Prophet(); $prophecy = $mockFw->prophesize(MathInterface::class); $prophecy->sum(1, 1)

    ->willReturn(2) ->shouldBeCalledTimes(1); $math = $prophecy->reveal();
  8. Mockery $math = Mockery::mock(MathInterface::class); $math->shouldReceive('sum') ->once() ->with(1, 1) ->andReturn(2);

  9. vfsStream baseDir = vfsStream::setup('dir'); mkdir(vfsStream::url('dir') . '/' . 'myDir'); Mooooock?

  10. bovigo/callmap math = NewInstance::of(MathInterface::class) ->mapCalls(['sum' => 2]); // ... verify($math,

    'sum')->wasCalledOnce();
  11. Phake $math = Phake::mock(MathInterface::class); Phake::when($math)->sum(1, 1)->thenReturn(2); // ... Phake::verify($math, Phake::times(1))->sum(1,

    1);
  12. Examples Let's mock mock mock: https://github.com/SenseException/mock-mock-mock

  13. Thank you Thank you Claudio Zizza @SenseException