Skip to content

Instantly share code, notes, and snippets.

@DaveLiddament
Created February 20, 2019 18:31
Show Gist options
  • Save DaveLiddament/d8ac3b72b539b7ee80db45379d348786 to your computer and use it in GitHub Desktop.
Save DaveLiddament/d8ac3b72b539b7ee80db45379d348786 to your computer and use it in GitHub Desktop.
Stubbing and Mocking notes

Stubbing and Mocking workshop

Setup

Clone https://github.com/bristol-php-training/php7-phpunit-password-validator

Run composer install and then vendor/bin/phpunit. Make sure basic test works.

Exercises

Exercise 1

Provide tests and solution for the method PasswordValidator::isValid based on docblock.

Talk

Introduce password strength requirement. Look at interface.

Benefit of design to interface.

Options:

  • Real
  • Double
    • Stub
    • Mock
    • Spy
    • Fake

Go through why we need to do a test double:

  • May not have real implementation
  • Real implementation changes
  • Don't know how to enter a password that scores 3 or 4.

Exercise 2

Hand written mock

  • Create test mock. Include counts
  • Add to existing tests
  • Create a new tests

Using mock builder

  • Show with minimal
  • Add in full mocks
  • Show problem should use stub

Introduce other concepts (check example branches)

  • Stubs
  • Mocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment