Skip to content

AndreyArthur/mockx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mockx

Mockx is a lightweight and intuitive mocking library for Go interfaces. It simplifies testing by allowing you to create mock implementations, define method behaviors, and capture method arguments with minimal boilerplate.

Motivation

This section reflects only the author's opinion. Whether you agree or disagree, you are encouraged to continue using the library as you see fit.

  • Dissatisfaction with the excessive boilerplate required to create mocks with other libraries, which practically forces the use of code generation.
  • Code generation feels like a hack rather than a proper solution in any sense.
  • Lack of clarity and directness when defining mock behaviors, especially due to the use of expects.
  • Using expects in mocks is conceptually wrong. If you find expects useful in your mocks, you are probably not writing unit tests. Additionally, integration tests should not use mocks.
  • Using a bunch of "anything" as expects feels even worse.

Installation

go get github.com/AndreyArthur/mockx

Features

  • Automatic Method Initialization: Generate zero-value implementations for all interface methods.
  • Method Behavior Mocking: Override methods with custom implementations or predefined return values.
  • Argument Capture: Easily retrieve arguments passed to mocked methods during tests.
  • Lightweight: No external dependencies and minimal setup required.
  • Example-Driven: Comprehensive examples included in tests for quick learning.

Usage

1. Define Your Interface

type Calculator interface {
    Add(a int, b int) int
}

2. Create a Mock Struct

Embed mockx.Mockx and implement the interface methods using Call:

type CalculatorMock struct {
    mockx.Mockx
}

func (m *CalculatorMock) Add(a int, b int) int {
    values := m.Call("Add", a, b)
    return mockx.Value[int](values[0])
}

3. Initialize the Mock

Use Init to auto-generate zero-value implementations for all methods:

calculator := &CalculatorMock{}
calculator.Init((*Calculator)(nil)) // Pass a nil interface pointer

4. Mock Method Behaviors

Set a Custom Implementation

calculator.Impl("Add", func(a int, b int) int {
    return a + b
})

Define Return Values

calculator.Return("Add", 42) // Always returns 42

Capture Arguments

calculator.Add(1, 2)
args := calculator.Args("Add") // Returns [1, 2]

Examples

Basic Mock Setup

// Initialize mock and use default zero values
calculator := &CalculatorMock{}
calculator.Init((*Calculator)(nil))

result := calculator.Add(3, 4) // Returns 0 (default)

Override Method Implementation

calculator.Impl("Add", func(a, b int) int {
    return a * b // Change behavior to multiply
})

result := calculator.Add(3, 4) // Returns 12

Force Specific Return Value

calculator.Return("Add", 100)

result := calculator.Add(5, 5) // Returns 100, ignores inputs

Retrieve Method Arguments

calculator.Add(10, 20)
args := calculator.Args("Add") // [10, 20]

License

MIT License. See LICENSE for details.

About

Mockx is a lightweight and intuitive mocking library for Go interfaces. Targeting simplicity and directness.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages