Mock Vs Stub Vs Fake: Understand The Difference

Cover Image for Mock vs Stub vs Fake: Understand the difference
Background

Arindam

Table of Contents
Table of Contents

Introduction

Testing software is like putting it through a series of challenges to make sure it’s tough enough for real-world use. Whether we’re testing each piece individually (unit testing) or how they all work together (integration testing), we need to be prepared for different situations.

Sometimes, testing is tricky.There are times, when we need to isolate parts of our code from their dependencies. Also When we examine how different pieces of our application interact with each other.That’s where tools like Mocks, Stubs, and Fakes come into the picture.

In this Article, We’ll explore What is Mock vs Stub vs Fake and their differences.

So without delaying further, Let’s dive deep into it!

Fakes Vs Stubs Vs Mocks

Fakes are a way for us to simulate objects and method functionality in our code without actually depending on a real object or method.

Stubs are a type of fake that quietly fakes behavior and can return a predefined expected value, while a mock is a type of fake that we can monitor to make sure that a certain method was called an expected amount of times or with an expected set of parameters.

In conclusion, while all three—Mock vs Stub vs Fake—serve as valuable tools in software testing, their differences lie in their roles. In the following sections we’ll explore more about this.

What Is Mock Testing?

Mock testing is a testing technique that is used in software development to simulate the behavior of components that a software module depends on.

Let’s say we have a function that sends an email to a user when they register on a website. In our unit test for this function, we want to ensure that the email is sent correctly without actually sending an email every time we run the test.

Instead, we can use a mock email service that replicates sending emails. It helps us to check if our program is working correctly or not!

What Is a Stub?

In simple words, A Stub is a simplified version of a software component used in testing that imitates the behavior of a service or object.

Suppose, We’re developing a software application that relies on a database connection, but the database system isn’t ready yet.

Now we’ll create a stub function that simulates the behavior of the database connection without actually performing any database operations. This stub function will return dummy data or predefined responses to mimic the expected behavior of the real database connection.

Thus, we can work on the testing part without thinking much about the depenencies.

What is Fake?

Just like stubs, A fake is a simplified version of a real component used in testing to mimic its behavior. Fakes typically have working implementations of all methods but may simplify complex behaviors or dependencies to make testing easier.

For example, a fake database might store data in memory instead of a physical database, or a fake HTTP client might return predefined responses instead of making actual network requests. Fakes help isolate code for testing and make tests faster and more predictable.

Why Do We Need Fake, Mock, and Stub?

When the complexity of the project increases, the need of Fake, Mock and Stub increases.Testing applications with intricate dependencies can be challenging for testers.

Fakes, Mocks, and Stubs offer valuable tools for simplifying this process,It enables more effective testing practices and improves the quality and reliability of software applications.

By understanding the distinctions between these concepts and leveraging them effectively, We, developers can make test our softwares easily.

Conclusion

If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on Javascript, React, and other web development topics.

To sponsor my work, please visit: Arindam’s Sponsor Page and explore the various sponsorship options.

Connect with me on Twitter, LinkedIn, Youtube and GitHub.

Thank you for Reading 🙂

Author

  • Arindam

    As a DevRel Intern at Keploy, a tool for automating writing manual Test cases, I am responsible for creating and delivering engaging and informative web content and technical documentation for our users and partners. I use my skills in web development, content creation, and public relations to produce high-quality tutorials, blogs, videos, podcasts, and newsletters that showcase the features and benefits of our platform and help our users solve their deployment challenges. I am also a passionate tech blogger and an open-source enthusiast, with over 50 blog posts published on various platforms, reaching over 100,000 views and 10,000 claps. I write on topics such as open source, web development, and data structures and algorithms, to share my knowledge and passion with the tech community. I have also participated in several open source programs and events, such as Hacktoberfest, Google Summer of Code, and MLH Fellowship, where I have contributed to various open source projects and learned from experienced mentors and peers. I am currently pursuing my B.Tech degrees in Information Technology and Computer Science at MAKAUT and Heritage Institute of Technology, respectively. I am eager to learn new skills and technologies and apply them to real-world problems and solutions. I am also interested in exploring the fields of artificial intelligence, machine learning, and natural language processing, and how they can enhance the user experience and the social impact of web applications. My goal is to become a full-stack developer and a leader in the tech industry.

Author Avatar
Author Details
Author Name: Arindam
Author Description:

  • More Stories

    Cover Image for Write Clean and Efficient Table driven Unit Tests in Go

    Write Clean and Efficient Table driven Unit Tests in Go

    Animesh Pathak

    Table of Contents Table driven tests, also known as parameterized tests, have became very popular over the past few years,...

    Cover Image for Canary Testing: A Comprehensive Guide for Developers

    Canary Testing: A Comprehensive Guide for Developers

    Animesh Pathak

    Table of Contents What’s Canary Testing, Anyway? Imagine you’re a miner with a canary in a cage. If the air...

    Cover Image for Mock vs Stub vs Fake: Understand the difference

    Mock vs Stub vs Fake: Understand the difference

    Arindam

    Table of Contents Introduction Testing software is like putting it through a series of challenges to make sure it’s tough...

    Cover Image for Writing test cases for Cron Job Testing

    Writing test cases for Cron Job Testing

    Animesh Pathak

    Table of Contents Understanding Cron Jobs: A Quick Recap Cron is a time-based job scheduler in Unix-like operating systems. It...

    Cover Image for Improving Code Quality and Accelerating Development: The Continuous Testing Way

    Improving Code Quality and Accelerating Development: The Continuous Testing Way

    Prajwal

    Table of Contents Introduction In the fast-changing world of software development, teams struggle to maintain good code quality while shortening...

    Cover Image for Understanding Testing in production

    Understanding Testing in production

    Arindam

    Table of Contents Introduction Testing in production was previously ignored by Product Developers, But recently it gaining Popularity Again! Even,...

    Cover Image for 5 Unit Testing Tools You Must Know in 2024

    5 Unit Testing Tools You Must Know in 2024

    Arindam

    Table of Contents Introduction: Unit testing is one of the most important areas to ensure code coverage and basic testing...

    Cover Image for Exploring Various Protocols : HTTP to Databases

    Exploring Various Protocols : HTTP to Databases

    Shivam

    Table of Contents Table of Contents Unraveling the Mystery of various Protocols The Role of Protocols in Network Communication Examples:...

    Cover Image for Demystifying Cron Job Testing

    Demystifying Cron Job Testing

    Animesh Pathak

    Table of Contents What is Cron Job? Before we dive into the world of testing Cron jobs, let’s first know...

    Cover Image for Building Custom YAML-DSL in Python

    Building Custom YAML-DSL in Python

    Animesh Pathak

    Table of Contents In this blog post, I will guide you through the process of building a custom DSL in...

    Cover Image for MongoDB in Mock Mode: Acting the Server Part

    MongoDB in Mock Mode: Acting the Server Part

    Ritik

    Table of Contents In the contemporary software development landscape, unit tests have become paramount for ensuring software quality. A prevalent...

    Cover Image for Capture gRPC Traffic going out from a Server

    Capture gRPC Traffic going out from a Server

    Mehfooz

    Table of Contents How does gRPC work? A quick Google search would tell you that it uses HTTP/2.0 under the...

    Cover Image for Integration vs E2E Testing: What worked for me as a charm

    Integration vs E2E Testing: What worked for me as a charm

    Sarthak Shyngle

    Table of Contents When it comes to testing software applications, various testing techniques can be employed. Three common testing methods...

    Cover Image for Automated E2E tests using Property Based Testing  | Part II

    Automated E2E tests using Property Based Testing | Part II

    charan

    Table of Contents If you haven’t visited Part I, I highly recommend you go through it for a better understanding...

    Cover Image for Automated End to End tests using Property Based Testing  | Part I

    Automated End to End tests using Property Based Testing | Part I

    charan

    Table of Contents " Engineers call them edge cases. I call them: what our users do " – Noah Sussman...

    Cover Image for Go Mocks and Stubs generator Made Easy

    Go Mocks and Stubs generator Made Easy

    Jain

    Table of Contents Testing network stuff like APIs and database calls can be a real pain: I find myself burning...