What you want to do in unit test, make sure that the method does the job that it should do. If a method uses dependencies to do this work, you will mock these dependencies and make sure that your method calls methods on the objects it depends on with the appropriate arguments. This way you check your code separately.
One of the advantages of this is that it will drive the design of your code in a better direction. For example, to use mockery, you naturally gravitate towards more untied code using dependency injection. This gives you the ability to easily replace your mock objects with the actual objects your class depends on. You also end up implementing interfaces that are more naturally ridiculed. Both of these are good design patterns and will enhance your code.
To test your specific example, for example, your class may depend on the factory for creating database connections and the builder for creating parameterized SQL commands that are executed through the connection. You will pass these mocked versions of these objects to your class and make sure that the correct methods for setting up the connection and command, assembling the correct command, executing it, and breaking the connection were installed. Or perhaps you add an already open connection and just create a command and call it. The point is that your class is built against an interface or a set of interfaces, and you use the mockery of supplying objects that implement these interfaces, and can record calls and provide the correct return values ββto the methods that you expect to use from the interface (s).
tvanfosson
source share