I have a function say
Myfunction() {
x.RunAsync() //Async Function call
}
I have to write a test case for Myfunction()
. I am using go-mock for this. Also have mocked x
.
Test_MyFunction() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
//EXPECT() call to stub RunAsync()
}
Problem now is my test runs successfully, but somehow at the end it panics saying, that call to mockX.RunAsync()
is missing.
I think this is because defer is being executed before my RunAsync was stubbed.
How do I ensure that all Async functions run before defer is executed.
If the test double is being used as a stub, then you should call .AnyTimes()
on it. This way gomock won't care if the go routine has finished or not.
If however you are wanting to make assertions on it (and therefore its a mock and not a stub), then you need to ensure the go routine has executed before letting the test finish.