A mocking framework is used to create replacement objects. These objects can be Fakes, Stubs, and Mocks. In unit testing scenarios, developers use mocking frameworks to isolate dependencies. This allows for a quick, concise, and reliable way to ensure that the testing goes smoothly.
More Background on Using Mocking Frameworks
Like discussed above, mock frameworks are extensions testing techniques for object oriented programming. So what problem does it solve? Let’s find out.
Say that you are testing your code that is still in development. In order to achieve the right results, you need to test its interactions with system resources, outside applications, and other dependencies. Unfortunately, you learn early on that that is not possible. Utilizing a mock framework allows for realistic emulations of the required interactions.
Creating a Mock
When you are running a test, they have to be set up. Oftentimes, dependencies are still under development and can require a set response, ie., a specific time of day, or error code. Even when you feel good that the code is interacting with external program or different system components, the tests can cannot be counted on.
With mock, they interact with the code as interfaces, meaning that you must write a fully-interface compliant test. When this is done properly, the code will not be able to tell the difference between the mock and the actual object.
Need a visual? Check out this Pluralsight video on how to Create mock objects with .NET
So, should you start mocking frameworks for testing? The answer to that is totally up to you. It matters what you are testing and how in depth of an interaction you are looking to get. Mindfire Technology loves looking at different ways to make writing and testing code more efficiently. Ask us what cool new tech we are working on, and we will answer!
You may have heard about "The Joel Test" before. It's a checklist of sorts, of things that you should look for before joining another software team. This checklist was written more than 17 years ago! What might we change or include if we were to make a new list today?
So you're unit testing, and perhaps even practicing Test Driven Development (TDD). Congratulations! You're likely better than at least half of your peers! There is just one thing you cannot figure out. How am I supposed to just test one thing when my code has dependencies? Enter Mocking!