mockverify
About 241 wordsLess than 1 minute
2025-01-16
Ensures mock implementations have compile-time interface verification.
Category
Testability
What It Checks
This analyzer detects mock implementations that don't verify they implement their interface at compile time.
Why It Matters
Without compile-time verification, interface changes don't cause compilation errors in mocks. Tests pass with incomplete mocks, then fail mysteriously at runtime.
Examples
Bad: No Verification
type MockStorage struct {
GetFunc func(key string) (string, error)
}
func (m *MockStorage) Get(key string) (string, error) {
return m.GetFunc(key)
}
// If Storage interface changes, this still compiles!Good: Compile-Time Verification
type MockStorage struct {
GetFunc func(key string) (string, error)
SetFunc func(key, value string) error
DeleteFunc func(key string) error
}
// Compile-time check - fails if interface changes
var _ Storage = (*MockStorage)(nil)
func (m *MockStorage) Get(key string) (string, error) {
return m.GetFunc(key)
}
func (m *MockStorage) Set(key, value string) error {
return m.SetFunc(key, value)
}
func (m *MockStorage) Delete(key string) error {
return m.DeleteFunc(key)
}The Verification Pattern
var _ InterfaceName = (*MockTypeName)(nil)This:
- Creates a nil pointer of the mock type
- Assigns it to the interface type
- Fails compilation if the mock doesn't implement the interface
Configuration
# .golint-sl.yaml
analyzers:
mockverify: true # enabled by defaultWhen to Disable
- Using mock generation tools that handle this automatically
analyzers:
mockverify: falseRelated Analyzers
- interfaceconsistency - Interface implementations
- clockinterface - Time interface pattern
