While writing unit test for a method in go, I am stumped at a problem. First, code snippet under test:
func MehodToBeTested(e Entity) {
go saveAudit(e)
//do something on which assertions can be done
}
Entity can be mocked. in the saveAudit method, Entity.Save method is called. In my UT, i want to assert that Entity.Save method is called once. Following is my current UT :
func TestMethod(t *testing.T) {
var mock = &mockEntity{}
mock.On("Save").Return(nil)
//make call to func under test
MethodToBeTested(mock)
// Assert that Save is called on Entity
mock.AssertNumberOfCalls(t, "Save",1)
}
This is giving error saying : Expected number of calls (1) does not match the actual number of calls (0) since the actual call is happening in another go routine. How can i test this?