I am writing the following unit test:
type mockQux struct {
mock.Mock
MockInterface
calculateHash func(ctx context.Context, foo *entities.Foo, bar model.Bar, baz *model.Baz) (uint64, error)
}
type MockInterface interface {
On(methodName string, arguments ...interface{}) *mock.Call
AssertExpectations func(t mock.TestingT)
}
func (m *mockQux) TestCalculateHash(t *testing.T) {
mq := new(mockQux)
ctx := context.Background()
foo := &entities.Foo{
// foo's fields
}
// create instances of model.Bar and &model.Baz the same way I created foo variable
mq.On("calculateHash", ctx, foo, bar, baz).Return(uint64(123), nil)
hash, err := m.Mock.calculateHash(ctx, foo, bar, baz)
assert.Equal(t, uint64(123), hash)
assert.Nil(t, err)
m.Mock.AssertExpectations(t)
}
func TestCalculateHash(t *testing.T) {
m := new(mockQux)
m.TestCalculateHash(t)
}
Running go test
and go test -c
, why do I get a segfault on On
and AssertExpectations
?
I've tried debugging this with delve, but it crashes whenever I run it, even if I instruct it to step over the problematic lines. I guess the problem might be that I improperly reference the variables in arguments to mq.On
or that I don't initialize all the fields (these structs are pretty complex).