-
Notifications
You must be signed in to change notification settings - Fork 608
Default expectations and override #137
Comments
I have similar question. By default I want
And in some test cases, I want to enforce a particular time
However, given the first is defined, the second one seems not to work |
Probably a bit late here, but for anyone coming upon this in the future, one pretty reasonable workaround that I just used here involves leveraging the order of precedence as-is to set defaults. That is, since earlier expectations take precedence over later ones, overrides work if you set them before filling in defaults:
|
I think the last comment sums up a way to do this quite well. Is it reasonable to close this issue @ybbus? |
Sorry its not clear how the last comment addresses the issue. The last comment seems to reiterate the problem statement again i.e. once EXPECT is defined you cannot change its definition while the ask is to allow change of the definition // assuming true is the positive case // now to test a negative flow I want it to return false but this second EXPECT doesnt seem to take affect ? |
@reversekick The way I solved this was a hint from StackOverflow - TLDR: Don't use AnyTimes() in EXPECT and make that EXPECT call get discarded after single use. Use a "prepareTest" function that can be called before the test case to set the EXPECT statement for positive or negative test case. This makes sense because AnyTimes() will instruct GoMock to return the same result multiple times. |
I don't think this is caused by using AnyTimes(), e.g. My default case, where I create my struct with dependencies for the test myMockDependency.EXPECT().Something(gomock.Any()).Times(1).Return(nil) In my test where I want to change this behaviour to make it return something different: myMockDependency.EXPECT().Something("foo").Times(1).Return(errors.New("something bad happened")) What I see:
It seems like this could be solved by adding a function on the generated controllers to clear any expectations? |
I came across this issue as I was looking for ways to simplify my error condition tests, which essentially involve constructing mocks such that in test1 I wasn't able to get the example from @andrewmains12 working, so I suspect something in Gomock has changed since it was first written. Gomock now expects both calls to be received, and so fails with the error:
A simple fix is to append
This change would loosen a test slightly - as it's no longer asserting the exact number of calls to the dependency - but generally speaking failing to call the mock the right number of times would cause a subsequent expectation to fail |
The best way I think:
mocker1.ExpectedCalls = suite.mocker1.ExpectedCalls[:0]
mocker1.EXPECT().Foo().Return(xxx) |
When you do a lot of tests with different dependencies it would be great if you could do the following:
Define a default behaviour (e.g.)
mockA.Expect().Function1().AnyTimes().Return(1)
mockA.Expect().Function2().AnyTimes().Return(2)
mockB.Expect().Function1().AnyTimes().Return(1)
mockB.Expect().Function2().AnyTimes().Return(2)
And most of the time in a single test you want to test a behaviour of your component if one of the default functions return something else (e.g. an error)
Therefore it would be great to override an expectation with a new one (or at least to clear expectations for a function)
So you could define a default behaviour of all functions (e.g. success case) and be able to override single functions that should behave different in specific tests.
Or is there a best practive for this?
The text was updated successfully, but these errors were encountered: