Prologue

I have been extremely lucky to be associated with art ever since I was a child. Art makes me calm. There’s much that’s left unsaid yet understood and that’s the beauty of it all. There’s one thing…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




What Is the difference Between a Mock and a Stub?

Often you’ll see Mocks and Stubs called interchangeably, however they are different, and sometimes not used correctly

Test-Driven Development relies heavily upon properly defining business cases where the requirements are translated to Test Units which derivate in short development cycles that promote the code to pass the tests one by one.

This philosophy of development presents itself as a challenge for many developers who are used to a more rapid-like development process in which the achievement of discernible goals takes precedence over a well-thought software architecture (as opposed to the measure twice cut once so to speak).

In that vein, it’s important to grasp the key concepts of a test-centered development. Two definitions that often get confused and sometimes are used interchangeably are Mocks and Stubs. Let’s dig deeper into the concepts of each and how they measure and contrast each other.

Semantically speaking, a Unit Test should concentrate on a single fragment (or unit) of a system. Yet, more often than not, you can’t test a single piece of your software without the aid of external elements, be a call to a third-party module, a database, or even another class that lives within your own codebase. In order for us to appropriately test the correctness of functionality, we will implement a representation (often called fakes or doubles) of these external element’s behaviors, responses, datasets, etc.

Stubs, traditionally, deliver a response for a call that was predetermined within the test. In other words, when a Test calls for a Stub, the response send back was actually engineered to accommodate the test, no actual connections were made to another web service or database, no logical computation was done internally, it just provides an expected response –or a state.

Meanwhile, a Mock deals more directly with the behavior of whatever it represents. You will usually see a Mock performing a bit of logic in it to adhere more closely to the object it is mocking.

At this point, I need to make a distinction between State validation and Behavior validation.

A State validation asserts that the value of a property, a variable, or response is equal to the expectation. An example of this might be to verify that a function returns the value we want, or perhaps that we receive the expected recordset when querying a database.

Meanwhile, a Behavior validation arguably will go further than the State validation, because it will also check if the actions and events expected to be triggered by the Test were set off. An example of a Behavior validation is to assert if a method has been called.

Mocks always validate behaviors. Stubs can verify behaviors as well, but then their name change to Spies.

Let’s assume that our business case is one financial software that holds account balances. For simplicity's sake, let’s assume a naïve function that withdraw()s an amount from the customer’s balance. The outcome can be twofold: if the balance is greater than the withdrawal amount, then the amount is deducted from the balance. If the withdrawal amount is greater than the balance, then nothing happens and the balance is kept the same.

Let’s see this using Python. It will be easy to follow even if you are not familiar with Python.

If you are curious about the class that we are testing, here it is:

Now, speaking about the two Unit Tests we are starting with, both are actually validating the State of the account’s balance. However, both are testing with the actual methods of the Account class –the real thing. Let’s try and test using a Stub.

As much as I would love to say with confidence “this is the way to go”, the truth is that you should analyze your cases individually and decide if you want to test using Stubs or Mocks.

On the one hand, Stubs tend to be closer to the Unit Test ideology of detaching the Unit of work that you want to test. By Stubbing your resources, you are effectively disconnecting your Tests from any possible side effects of an outsider. On the other hand, eventually, as your codebase grows, you will find that updates in the implementation of a method could swell into a cascade of changes in your Stubs.

When you Mock your outside resources, it helps you think in a more holistic way and evaluate the interactions between the domains of your software. Also, and probably in a more practical sense, they can help you achieve higher coverage over intricate pieces of code that normally won’t get tested often or more than once. One downside of Mocking is that you are, at some level, coupling your Tests with outside functionality, breaking the boundaries of your Unit Tests.

In practice, it’s probably a good idea to go with a combination of the two, considering your cases one by one.

Learn different ways to expand and leverage your knowledge in code testing:

Add a comment

Related posts:

California Fire Prompts Evacuation

California Fire Prompts Evacuation. Here are the key facts of this story, as agreed upon by Associated Press, Guardian, US News, NPR Online News, and Al Jazeera..

Success stories of young entrepreneurs who made it

Cameron fellows is a 20-year-old filmmaker from Bury St Edmunds, studying Film and TV production in Cambridge. He started his own production company in 2016 when he was in last year of school…

Track The Way To Find Best House Removal Service

Different people relocate for a variety of reasons. While some do it owing to a lack of room in their current home, others do it for academic and official reasons. Moving to a new location, for…