With the flattened structure, mockery can now easily generate mocks for all interfaces without any circular dependency issues.
Mocks are named after their corresponding interface files:
git.go→ ContainsMockGitfor the Git interfacecommand.go→ ContainsMockCommandfor the Command interfacesession.go→ ContainsMockSessionfor the Session interface
# Generate Git interface mock → pkg/git/mocks/git.go
mockery --dir=pkg/git --name=Git --output=pkg/git/mocks --filename=git.go
# Generate Session interface mock → pkg/git/mocks/session.go
mockery --dir=pkg/git --name=Session --output=pkg/git/mocks --filename=session.go
# Generate Command interface mock → pkg/git/mocks/command.go
mockery --dir=pkg/git --name=Command --output=pkg/git/mocks --filename=command.goUse the provided .mockery.yaml config:
mockeryThis generates:
pkg/git/mocks/git.gopkg/git/mocks/command.gopkg/git/mocks/session.go
package mypackage_test
import (
"testing"
"github.com/instruqt/git-exec/pkg/git"
"github.com/instruqt/git-exec/pkg/git/mocks"
"github.com/stretchr/testify/assert"
)
func TestWithMocks(t *testing.T) {
// Create mock (note the Mock prefix)
mockGit := mocks.NewMockGit(t)
// Set up expectations
mockGit.On("Add", []string{"file.txt"}).Return(nil).Once()
// Use mock
err := mockGit.Add([]string{"file.txt"})
assert.NoError(t, err)
// Verify expectations (automatic cleanup)
mockGit.AssertExpectations(t)
}- No Circular Dependencies: Clean imports allow mockery to work flawlessly
- Organized Mock Files: Each interface gets its own mock file matching the source
- Simple Commands: Just
mockery --dir=pkg/git --name=InterfaceName - All Interfaces Mockable: Git, Session, Command - all can be mocked
- Clean Integration: Works perfectly with testify/mock patterns
- File Consistency: Mock files mirror the structure of interface files
- No Factory Patterns Needed: Direct interface usage for clean testing