|
| 1 | +# Mock-Based Testing for Better Coverage |
| 2 | + |
| 3 | +This document outlines how to implement mock-based unit tests to improve test coverage beyond acceptance tests. |
| 4 | + |
| 5 | +## Why Mock-Based Testing? |
| 6 | + |
| 7 | +- **Higher Coverage**: Test error paths and edge cases without live NetBox instance |
| 8 | +- **Faster Execution**: No network calls or Docker setup required |
| 9 | +- **Reliable**: Tests don't depend on external services |
| 10 | +- **Focused**: Test specific functions in isolation |
| 11 | + |
| 12 | +## Current Coverage Status |
| 13 | + |
| 14 | +Current test coverage: ~75.0% |
| 15 | + |
| 16 | +Areas that would benefit from mock tests: |
| 17 | +- API error handling in data sources and resources |
| 18 | +- Network failure scenarios |
| 19 | +- Authentication errors |
| 20 | +- Rate limiting |
| 21 | +- Malformed responses |
| 22 | + |
| 23 | +## Recommended Mock Testing Setup |
| 24 | + |
| 25 | +### 1. Choose a Mocking Framework |
| 26 | + |
| 27 | +```bash |
| 28 | +go get github.com/stretchr/testify/mock |
| 29 | +# or |
| 30 | +go get github.com/golang/mock/gomock |
| 31 | +``` |
| 32 | + |
| 33 | +### 2. Example Mock Test Structure |
| 34 | + |
| 35 | +```go |
| 36 | +package netbox |
| 37 | + |
| 38 | +import ( |
| 39 | + "errors" |
| 40 | + "testing" |
| 41 | + |
| 42 | + "github.com/fbreckle/go-netbox/netbox/client" |
| 43 | + "github.com/fbreckle/go-netbox/netbox/client/extras" |
| 44 | + "github.com/fbreckle/go-netbox/netbox/models" |
| 45 | + "github.com/stretchr/testify/assert" |
| 46 | +) |
| 47 | + |
| 48 | +// Mock client for testing |
| 49 | +type mockNetBoxClient struct { |
| 50 | + extrasAPI *mockExtrasAPI |
| 51 | +} |
| 52 | + |
| 53 | +type mockExtrasAPI struct { |
| 54 | + tagsListFunc func(*extras.ExtrasTagsListParams) (*extras.ExtrasTagsListOK, error) |
| 55 | +} |
| 56 | + |
| 57 | +func (m *mockExtrasAPI) ExtrasTagsList(params *extras.ExtrasTagsListParams, authInfo interface{}) (*extras.ExtrasTagsListOK, error) { |
| 58 | + if m.tagsListFunc != nil { |
| 59 | + return m.tagsListFunc(params) |
| 60 | + } |
| 61 | + return nil, errors.New("mock not implemented") |
| 62 | +} |
| 63 | + |
| 64 | +func TestFindTag_APIError(t *testing.T) { |
| 65 | + // Setup mock |
| 66 | + mockAPI := &mockExtrasAPI{ |
| 67 | + tagsListFunc: func(params *extras.ExtrasTagsListParams) (*extras.ExtrasTagsListOK, error) { |
| 68 | + return nil, errors.New("connection refused") |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + mockClient := &client.NetBoxAPI{} |
| 73 | + // Note: In practice, you'd need to properly inject the mock |
| 74 | + |
| 75 | + // This is a simplified example - actual implementation would require |
| 76 | + // interface extraction and dependency injection |
| 77 | + tag, err := findTag(mockClient, "test-tag") |
| 78 | + |
| 79 | + assert.Error(t, err) |
| 80 | + assert.Nil(t, tag) |
| 81 | + assert.Contains(t, err.Error(), "API Error") |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### 3. Implementation Strategy |
| 86 | + |
| 87 | +1. **Extract Interfaces**: Create interfaces for API clients to enable mocking |
| 88 | +2. **Dependency Injection**: Modify functions to accept interfaces instead of concrete types |
| 89 | +3. **Mock Generation**: Use code generation tools to create mocks |
| 90 | +4. **Test Organization**: Separate unit tests from acceptance tests |
| 91 | + |
| 92 | +### 4. Functions to Mock Test |
| 93 | + |
| 94 | +Priority order for mock testing: |
| 95 | + |
| 96 | +1. **Utility Functions** (already done) |
| 97 | + - `findTag` in `tags.go` |
| 98 | + - `getNestedTagListFromResourceDataSet` |
| 99 | + - `readTags` |
| 100 | + |
| 101 | +2. **Data Source Functions** |
| 102 | + - `dataSourceNetboxAsnsRead` - API errors, no results |
| 103 | + - `dataSourceNetboxTagRead` - multiple results, API errors |
| 104 | + |
| 105 | +3. **Resource Functions** |
| 106 | + - CRUD operations with API failures |
| 107 | + - 404 handling in read/delete |
| 108 | + - Validation errors |
| 109 | + |
| 110 | +4. **Provider Functions** |
| 111 | + - `providerConfigure` with invalid credentials |
| 112 | + - Version checking failures |
| 113 | + |
| 114 | +### 5. Benefits Expected |
| 115 | + |
| 116 | +- Coverage increase: 74.8% → 85%+ |
| 117 | +- Faster test execution |
| 118 | +- Better error path testing |
| 119 | +- Reduced CI resource usage |
| 120 | + |
| 121 | +## Getting Started |
| 122 | + |
| 123 | +1. Start with simple functions like `findTag` |
| 124 | +2. Extract interfaces for API clients |
| 125 | +3. Use testify/mock for simple mocking |
| 126 | +4. Gradually expand to more complex scenarios |
| 127 | + |
| 128 | +## Example Implementation |
| 129 | + |
| 130 | +See `tags_mock_test.go` for a basic mock test example (requires interface extraction for full implementation). |
0 commit comments