Build a test stub to both fake the API of another object that our class depends on, as well as providing a mocked results from our test stub to have our faked object do work.
- View Offline
- [Instructor] Now, we'll build the next feature…in our Receipt library,…which will take the amount from our total call…and the amount from our tax call and sum them together…and return the final total for our receipt.…Now, we'll build the next feature in your Receipt library,…which will take the amount from our total call…and the amount from our tax call and sum them together…and return the final amount for our receipt.…To do this, we'll add a method, postTaxTotal,…that calculates the total for a collection of items…and the tax total, and then sums those two values together.…
First, open the ReceiptTest.php class…in our tests directory.…Next, add a function call,…public function TestPostTaxTotal, on line 37.…Our test here will build a mock instance…of the Receipt class…so we can replace the instance we are using…and instead call it,…and then return the sum from the two calls…to the other methods,…so we're testing this in isolation.…Let's build this out and it'll start to make more sense.…
Building a mock PHPUnit is fairly easy.…
- Why use unit testing?
- Writing unit tests
- Extending unit tests
- Filtering PHPUnit tests
- Building dummy objects
- Working with data providers
- Writing an exception-based test
- Using TDD tactics
- Using PHPUnit advanced tactics, such as database tests