Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit testing in general

Unit testing in general

Adam

May 23, 2015
Tweet

More Decks by Adam

Other Decks in Programming

Transcript

  1. In a nutshell public function testArrayPush() { $stack = array();

    array_push($stack, 'foo'); $this->assertEquals('foo', $stack[0]); } Precondition Execution Assertion
  2. Unit? “really it's a situational thing - the team decides

    what makes sense to be a unit” - Martin Fowler
  3. Fundamental to regression testing Required for refactoring Can be a

    good documentation Finds problems early Unit tests / Pros
  4. Hard-coded dependencies Static method calls Methods with too many responsibilities

    Tight coupling Complex methods Unreadable code “Untestable” code
  5. Static method calls class Foo { public static function doSomething()

    { return static::helper(); } public static function helper() { return 'foo'; } }
  6. Hard-coded dependencies protected function newCallback($className) { switch ($className) { case

    'Bar': return 'BarMock'; default: return $className; } } set_new_overload(array($this, 'newCallback')); http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html
  7. Static method calls class Foo { public static function doSomething()

    { return static::helper(); } public static function helper() { return 'foo'; } }
  8. Static method calls $class = $this->getMockClass( 'Foo', # class to

    mock array('helper') # methods to mock ); $class::staticExpects($this->any()) ->method('helper') ->will($this->returnValue('bar')); https://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html
  9. !

  10. Use dependency injection Try to avoid static method calls SRP

    Readable KISS, DRY, SOLID Testable code
  11. Mock your dependencies “…mock objects are simulated objects that mimic

    the behavior of real objects in controlled ways”