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.
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.
go get github.com/AndreyArthur/mockx- 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.
type Calculator interface {
Add(a int, b int) int
}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])
}Use Init to auto-generate zero-value implementations for all methods:
calculator := &CalculatorMock{}
calculator.Init((*Calculator)(nil)) // Pass a nil interface pointercalculator.Impl("Add", func(a int, b int) int {
return a + b
})calculator.Return("Add", 42) // Always returns 42calculator.Add(1, 2)
args := calculator.Args("Add") // Returns [1, 2]// Initialize mock and use default zero values
calculator := &CalculatorMock{}
calculator.Init((*Calculator)(nil))
result := calculator.Add(3, 4) // Returns 0 (default)calculator.Impl("Add", func(a, b int) int {
return a * b // Change behavior to multiply
})
result := calculator.Add(3, 4) // Returns 12calculator.Return("Add", 100)
result := calculator.Add(5, 5) // Returns 100, ignores inputscalculator.Add(10, 20)
args := calculator.Args("Add") // [10, 20]MIT License. See LICENSE for details.