-
Notifications
You must be signed in to change notification settings - Fork 175
/
unittest.go
52 lines (41 loc) · 1.6 KB
/
unittest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package irrecoverable
import (
"context"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// MockSignalerContext is a SignalerContext that can be used in tests to assert that an error is thrown.
// It embeds a mock.Mock, so it can be used it to assert that Throw is called with a specific error.
// Use NewMockSignalerContextExpectError to create a new MockSignalerContext that expects a specific error, otherwise NewMockSignalerContext.
type MockSignalerContext struct {
context.Context
*mock.Mock
}
var _ SignalerContext = &MockSignalerContext{}
func (m MockSignalerContext) sealed() {}
func (m MockSignalerContext) Throw(err error) {
m.Called(err)
}
func NewMockSignalerContext(t *testing.T, ctx context.Context) *MockSignalerContext {
m := &MockSignalerContext{
Context: ctx,
Mock: &mock.Mock{},
}
m.Mock.Test(t)
t.Cleanup(func() { m.AssertExpectations(t) })
return m
}
// NewMockSignalerContextWithCancel creates a new MockSignalerContext with a cancel function.
func NewMockSignalerContextWithCancel(t *testing.T, parent context.Context) (*MockSignalerContext, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)
return NewMockSignalerContext(t, ctx), cancel
}
// NewMockSignalerContextExpectError creates a new MockSignalerContext which expects a specific error to be thrown.
func NewMockSignalerContextExpectError(t *testing.T, ctx context.Context, err error) *MockSignalerContext {
require.NotNil(t, err)
m := NewMockSignalerContext(t, ctx)
// since we expect an error, we should expect a call to Throw
m.On("Throw", err).Once().Return()
return m
}