国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development C++ MockManager in unit tests - a builder pattern used for mocks

MockManager in unit tests - a builder pattern used for mocks

Dec 19, 2024 pm 12:27 PM

MockManager in unit tests - a builder pattern used for mocks

A few years ago I wrote about this, but in less detail. Here is a more refined version of the same idea.

Intro

Unit tests are both boon and bane to developers. They allow quick testing of functionality, readable examples of use, fast experimentation of scenarios for just the components involved. But they also can become messy, need maintenance and update with every code change and, when done lazily, can't hide bugs rather than reveal them.

I think the reason unit testing is so difficult is because it's associated with testing, something other than code writing, and also that unit tests are written in a way opposite most other code we write.

In this post I will give you a simple pattern of writing unit tests that will enhance all the benefits, while eliminating most of the cognitive dissonance with normal code. Unit tests will remain readable and flexible, while reducing duplicate code and adding no extra dependencies.

How to unit test

But first, let's define a good unit test suite.

To properly test a class, it has to be written in a certain way. In this post we will cover classes using constructor injection for dependencies, which is my recommended way of doing dependency injection.

Then, in order to test it, we need to:

  • cover positive scenarios - when the class does what it's supposed to do, with various combinations of setup and input parameters to cover the whole functionality
  • cover negative scenarios - when the class fails in the correct way when the setup or input parameters are wrong
  • mock all external dependencies
  • keep all of the test setup, action and assertion in the same test (what is normally called the Arrange-Act-Assert structure)

But that's easier said than done, because it also implies:

  • setting up the same dependencies for every test, thus copying and pasting a lot of code
  • setting up very similar scenarios, with just one change between two tests, again repeating a lot of code
  • generalizing and encapsulating nothing, which is what a dev would normally do in all of their code
  • writing a lot of negative cases for few positive cases, which feels like having more testing code than functional code
  • having to update all of these tests for every change to the tested class

Who loves that?

Solution

The solution is to use the builder software pattern to create fluid, flexible and readable tests in the Arrange-Act-Assert structure, while encapsulating setup code in a class complementing the unit test suite for a specific service. I call this the MockManager pattern.

Let's start with a simple example:

// the tested class
public class Calculator
{
    private readonly ITokenParser tokenParser;
    private readonly IMathOperationFactory operationFactory;
    private readonly ICache cache;
    private readonly ILogger logger;

    public Calculator(
        ITokenParser tokenParser,
        IMathOperationFactory operationFactory,
        ICache cache,
        ILogger logger)
    {
        this.tokenParser = tokenParser;
        this.operationFactory = operationFactory;
        this.cache = cache;
        this.logger = logger;
    }

    public int Calculate(string input)
    {
        var result = cache.Get(input);
        if (result.HasValue)
        {
            logger.LogInformation("from cache");
            return result.Value;
        }
        var tokens = tokenParser.Parse(input);
        IOperation operation = null;
        foreach(var token in tokens)
        {
            if (operation is null)
            {
                operation = operationFactory.GetOperation(token.OperationType);
                continue;
            }
            if (result is null)
            {
                result = token.Value;
                continue;
            }
            else
            {
                if (result is null)
                {
                    throw new InvalidOperationException("Could not calculate result");
                }
                result = operation.Execute(result.Value, token.Value);
                operation = null;
            }
        }
        cache.Set(input, result.Value);
        logger.LogInformation("from operation");
        return result.Value;
    }
}

This is a calculator, as is tradition. It receives a string and returns an integer value. It also caches the result for a specific input, and logs some stuff. The actual operations are being abstracted by IMathOperationFactory and the input string is translated into tokens by an ITokenParser. Don't worry, this is not a real class, just an example. Let's look at a "traditional" test:

[TestMethod]
public void Calculate_AdditionWorks()
{
    // Arrange
    var tokenParserMock = new Mock<ITokenParser>();
    tokenParserMock
        .Setup(m => m.Parse(It.IsAny<string>()))
        .Returns(
            new List<CalculatorToken> {
                CalculatorToken.Addition, CalculatorToken.From(1), CalculatorToken.From(1)
            }
        );

    var mathOperationFactoryMock = new Mock<IMathOperationFactory>();

    var operationMock = new Mock<IOperation>();
    operationMock
        .Setup(m => m.Execute(1, 1))
        .Returns(2);

    mathOperationFactoryMock
        .Setup(m => m.GetOperation(OperationType.Add))
        .Returns(operationMock.Object);

    var cacheMock = new Mock<ICache>();
    var loggerMock = new Mock<ILogger>();

    var service = new Calculator(
        tokenParserMock.Object,
        mathOperationFactoryMock.Object,
        cacheMock.Object,
        loggerMock.Object);

    // Act
    service.Calculate("");

    //Assert
    mathOperationFactoryMock
        .Verify(m => m.GetOperation(OperationType.Add), Times.Once);
    operationMock
        .Verify(m => m.Execute(1, 1), Times.Once);
}

Let's unpack it a little. We had to declare a mock for every constructor dependency, even if we don't actually care about the logger or the cache, for example. We also had to set up a mock method that returns another mock, in the case of the operation factory.

In this particular test we wrote mostly setup, one line of Act and two lines of Assert. Moreover, if we want to test how the cache works inside the class we would have to copy paste the entire thing and just change the way we setup the cache mock.

And there are the negative tests to consider. I've seen many a negative test doing something like: "setup just what is supposed to fail. test that it fails", which introduces a lot of problems, mainly because it might fail for completely different reasons and most of the time these tests are following the internal implementation of the class rather than its requirements. A proper negative test is actually a fully positive test with just one wrong condition. Not the case here, for simplicity.

So, without further ado, here is the same test, but with a MockManager:

[TestMethod]
public void Calculate_AdditionWorks_MockManager()
{
    // Arrange
    var mockManager = new CalculatorMockManager()
        .WithParsedTokens(new List<CalculatorToken> {
            CalculatorToken.Addition, CalculatorToken.From(1), CalculatorToken.From(1)
        })
        .WithOperation(OperationType.Add, 1, 1, 2);

    var service = mockManager.GetService();

    // Act
    service.Calculate("");

    //Assert
    mockManager
        .VerifyOperationExecute(OperationType.Add, 1, 1, Times.Once);
}

Unpacking, there is no mention of cache or logger, because we don't need any setup there. Everything is packed and readable. Copy pasting this and changing a few parameters or some lines is no longer ugly. There are three methods executed in Arrange, one in Act and one in Assert. Only the nitty gritty mocking details are abstracted away: there is no mention of the Moq framework here. In fact, this test would look the same regardless of the mocking framework one decides to use.

Let's take a look at the MockManager class. Now this will appear complicated, but remember that we only write this once and use it many times. The whole complexity of the class is there to make unit tests readable by humans, easily to understand, update and maintain.

public class CalculatorMockManager
{
    private readonly Dictionary<OperationType,Mock<IOperation>> operationMocks = new();

    public Mock<ITokenParser> TokenParserMock { get; } = new();
    public Mock<IMathOperationFactory> MathOperationFactoryMock { get; } = new();
    public Mock<ICache> CacheMock { get; } = new();
    public Mock<ILogger> LoggerMock { get; } = new();

    public CalculatorMockManager WithParsedTokens(List<CalculatorToken> tokens)
    {
        TokenParserMock
            .Setup(m => m.Parse(It.IsAny<string>()))
            .Returns(
                new List<CalculatorToken> {
                    CalculatorToken.Addition, CalculatorToken.From(1), CalculatorToken.From(1)
                }
            );
        return this;
    }

    public CalculatorMockManager WithOperation(OperationType operationType, int v1, int v2, int result)
    {
        var operationMock = new Mock<IOperation>();
        operationMock
            .Setup(m => m.Execute(v1, v2))
            .Returns(result);

        MathOperationFactoryMock
            .Setup(m => m.GetOperation(operationType))
            .Returns(operationMock.Object);

        operationMocks[operationType] = operationMock;

        return this;
    }

    public Calculator GetService()
    {
        return new Calculator(
                TokenParserMock.Object,
                MathOperationFactoryMock.Object,
                CacheMock.Object,
                LoggerMock.Object
            );
    }

    public CalculatorMockManager VerifyOperationExecute(OperationType operationType, int v1, int v2, Func<Times> times)
    {
        MathOperationFactoryMock
            .Verify(m => m.GetOperation(operationType), Times.AtLeastOnce);
        var operationMock = operationMocks[operationType];
        operationMock
            .Verify(m => m.Execute(v1, v2), times);
        return this;
    }
}

All of the required mocks for the test class are declared as public properties, allowing any customization of a unit test. There is a GetService method, which will always return an instance of the tested class, with all of the dependencies fully mocked. Then there are With* methods which atomically set up various scenarios and always return the mock manager, so that they can be chained. You can also have specific methods for assertion, although in most cases you will be comparing some output with an expected value, so these are here just to abstract away the Verify method of the Moq framework.

Conclusion

This pattern now aligns test writing with code writing:

  • abstract the things you don't care about in any context
  • write once and use many times
  • humanly readable, self documenting code
  • small methods with low cyclomatic complexity
  • intuitive code writing

Writing a unit test now is trivial and consistent:

  1. instantiate the mock manager of the class you want to test (or write one based on the steps above)
  2. compose specific scenarios for the test (with auto complete for existing already covered scenario steps)
  3. execute the method you want to test with test parameters
  4. check everything is as expected

The abstraction doesn't stop at the mocking framework. The same pattern can be applied in every programming language! The mock manager construct will be very different for TypeScript or JavaScript or something else, but the unit test would pretty much look the same way.

Hope this helps!

The above is the detailed content of MockManager in unit tests - a builder pattern used for mocks. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C   tutorial for people who know Python C tutorial for people who know Python Jul 01, 2025 am 01:11 AM

People who study Python transfer to C The most direct confusion is: Why can't you write like Python? Because C, although the syntax is more complex, provides underlying control capabilities and performance advantages. 1. In terms of syntax structure, C uses curly braces {} instead of indentation to organize code blocks, and variable types must be explicitly declared; 2. In terms of type system and memory management, C does not have an automatic garbage collection mechanism, and needs to manually manage memory and pay attention to releasing resources. RAII technology can assist resource management; 3. In functions and class definitions, C needs to explicitly access modifiers, constructors and destructors, and supports advanced functions such as operator overloading; 4. In terms of standard libraries, STL provides powerful containers and algorithms, but needs to adapt to generic programming ideas; 5

What is the Standard Template Library (STL) in C  ? What is the Standard Template Library (STL) in C ? Jul 01, 2025 am 01:17 AM

C STL is a set of general template classes and functions, including core components such as containers, algorithms, and iterators. Containers such as vector, list, map, and set are used to store data. Vector supports random access, which is suitable for frequent reading; list insertion and deletion are efficient but accessed slowly; map and set are based on red and black trees, and automatic sorting is suitable for fast searches. Algorithms such as sort, find, copy, transform, and accumulate are commonly used to encapsulate them, and they act on the iterator range of the container. The iterator acts as a bridge connecting containers to algorithms, supporting traversal and accessing elements. Other components include function objects, adapters, allocators, which are used to customize logic, change behavior, and memory management. STL simplifies C

C   tutorial for competitive programming C tutorial for competitive programming Jul 02, 2025 am 12:54 AM

Learn C You should start from the following points when playing games: 1. Proficient in basic grammar but do not need to go deep into it, master the basic contents of variable definition, looping, condition judgment, functions, etc.; 2. Focus on mastering the use of STL containers such as vector, map, set, queue, and stack; 3. Learn fast input and output techniques, such as closing synchronous streams or using scanf and printf; 4. Use templates and macros to simplify code writing and improve efficiency; 5. Familiar with common details such as boundary conditions and initialization errors.

C   tutorial on the Standard Template Library (STL) C tutorial on the Standard Template Library (STL) Jul 02, 2025 am 01:26 AM

STL (Standard Template Library) is an important part of the C standard library, including three core components: container, iterator and algorithm. 1. Containers such as vector, map, and set are used to store data; 2. Iterators are used to access container elements; 3. Algorithms such as sort and find are used to operate data. When selecting a container, vector is suitable for dynamic arrays, list is suitable for frequent insertion and deletion, deque supports double-ended quick operation, map/unordered_map is used for key-value pair search, and set/unordered_set is used for deduplication. When using the algorithm, the header file should be included, and iterators and lambda expressions should be combined. Be careful to avoid failure iterators, update iterators when deleting, and not modify m

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How to use cin and cout for input/output in C  ? How to use cin and cout for input/output in C ? Jul 02, 2025 am 01:10 AM

In C, cin and cout are used for console input and output. 1. Use cout to read the input, pay attention to type matching problems, and stop encountering spaces; 3. Use getline(cin, str) when reading strings containing spaces; 4. When using cin and getline, you need to clean the remaining characters in the buffer; 5. When entering incorrectly, you need to call cin.clear() and cin.ignore() to deal with exception status. Master these key points and write stable console programs.

C   tutorial for graphics programming with OpenGL C tutorial for graphics programming with OpenGL Jul 02, 2025 am 12:07 AM

As a beginner graphical programming for C programmers, OpenGL is a good choice. First, you need to build a development environment, use GLFW or SDL to create a window, load the function pointer with GLEW or glad, and correctly set the context version such as 3.3. Secondly, understand OpenGL's state machine model and master the core drawing process: create and compile shaders, link programs, upload vertex data (VBO), configure attribute pointers (VAO) and call drawing functions. In addition, you must be familiar with debugging techniques, check the shader compilation and program link status, enable the vertex attribute array, set the screen clear color, etc. Recommended learning resources include LearnOpenGL, OpenGLRedBook and YouTube tutorial series. Master the above

What is the volatile keyword in C  ? What is the volatile keyword in C ? Jul 04, 2025 am 01:09 AM

volatile tells the compiler that the value of the variable may change at any time, preventing the compiler from optimizing access. 1. Used for hardware registers, signal handlers, or shared variables between threads (but modern C recommends std::atomic). 2. Each access is directly read and write memory instead of cached to registers. 3. It does not provide atomicity or thread safety, and only ensures that the compiler does not optimize read and write. 4. Constantly, the two are sometimes used in combination to represent read-only but externally modifyable variables. 5. It cannot replace mutexes or atomic operations, and excessive use will affect performance.

See all articles