This week in class, we took another deep dive into the world of test doubles. This week pertained to mocks specifically. While doing class activities related to mocks this week in class, it seemed to me that mocks are easier to use than stubs while offering more insight on how a program is supposed to run. With that in my mind, I took the opportunity to further learn about mocks. I ended up finding a blog that gave me an even better understanding about mocks, and it is called “Understanding Mock Objects in Software Testing: A Tale of Simulated Reality”.
One quote from the blog does a great job of summing up what mocks are. “It’s like a stand-in actor in a movie – the mock object behaves like the real one but is controlled within the testing environment.” Mocks mimic the actions of the object it replaces when called upon in a test class. Mocks check the order and frequency of method calls in which they are used, which stubs don’t do.
Frameworks
There are mocking frameworks for many different languages. Some common mock frameworks include Mockito in Java, Moq in .NET, and Jasmine in JavaScript. Mockito is popular for its simplicity. It does well to create mock objects for a certain class or database that simulates how a real object would respond in the program. Moq is primarily used for C# applications and is popular for its strong typing and fluent interface. A language with strong typing demands specification over data types, so variables must have a type when they are defined. Jasmine mocks HTTP requests, which allows front-end applications to be tested without back-end interactions.
Pros
Some of the benefits of using mocks is that they are used in a controlled and predictable environment, and they are efficient. Mocks give predetermined behaviors and don’t leave the testing framework, which leads to consistency and predictability. And since mocks are used in the place of real-world objects, the testing process is sped up. It allows testers to isolate external factors and focus solely on the code being tested. Mock testing is also a cheaper alternative to testing real-world objects that may be very expensive.
Cons
A drawback of mock testing is that while tests pass in the testing, they may not in real-world situations. And just like stubs, changes to the system can cause mocks to become outdated, so it is important that mocks are frequently provisioned in order kept up to date with any changes to the system being tested.
Example
In the example above Mockito is the framework being used. WeatherService represents the external dependency, and WeatherModule represents what is being tested. mockService should return “Sunny” when “New York” is the string in the .getWeather() method. This allows WeatherModule to be tested in isolation. module.getWeather() is then assigned to the string variable “weather”. From there, weather is tested for if it returns “Sunny”. This is fascinating because the tests compile and are ran using make-believe objects
Reference
https://www.thetesttribe.com/blog/mock-testing/
From the blog Blog del William by William Cordor and used with permission of the author. All other rights reserved by the author.