What are testing doubles?

Before we get into the specifics about stubbing/mocking, it would be helpful to go over some industry-standard terminology. First of all, there is the concept of testing doubles. Interestingly, the term comes from a movie-making technique that's related to how stunts are filmed. When performing dangerous acts, a stuntman or stuntwoman replaces the actor or actress to perform the job. From a viewer's perspective, it would look like the original actor or actress was performing. Testing doubles are the same in the sense that a fake component is used in place of the real thing during testing.

There are multiple types of testing doubles, but the most useful ones are stubs and mocks, which we will focus on in this section. In object-oriented programming, these concepts are expressed in terms of classes and objects. In Julia, we will leverage the same terminology for functions. One benefit of working with functions is that we can focus all of our effort on testing a single thing.

A stub is a fake function that imitates the real function, also known as the collaborator function. Depending on what is required from the testing objectives, they can be as dumb as returning the same result all the time, or they can be a little smarter and return different values, depending on the input arguments. Regardless of how smart they are, return values are almost always hardcoded for consistency reasons. During testing, stubs replace the collaborator function when the function under test (FUT) is being exercised. When the FUT finishes its execution, we can determine the correctness of the returned value. This is called state verification. The interaction between these functions can be depicted as follows:

A mock is also a fake function that imitates the collaborator function. The difference between mocks and stubs is that a mock focuses on behavior verification. Rather than just examining the state of the FUT, mocks keep track of all the calls being made. It can be used to verify behavior, such as how many times the mock is expected to be called, the types and values of the arguments that the mock expected to be passed, and so on. This is called behavior verification. At the end of their execution, we can perform both state verification and behavior verification. This is depicted as follows:

In the upcoming sections, we will focus on how to apply stubs and mocks in testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.116.159