Now that you’ve learned about using mocks and stubs, complete these exercises to apply that knowledge to a sample application.
-
In the ex7 directory, the notifier.go file contains a type called NotificationService with a method called Notify. Use a function field stub to write a table test to make sure this code properly handles correct data, invalid input, and a failed send.
-
In the ex8 directory, the report.go file contains a type called ReportBuilder with a method called PriceSummary. ReportBuilder depends on a large interface (DataStore), but PriceSummary only used one method in the interface. Write a test to validate that PriceSummary works correctly.
-
In the ex9 directory, the weather.go file contains a type called Client with a method called GetTemperature, which connects to a remote HTTP server to get weather data. Write a unit test to validate this method that doesn’t require you to run an external server.