3 ms·
My goto simple example here for dumb mocks is a multiply function. You are doing it wrong if you have: MyMock.Mocks(myMultiplyFunc).WithArgs(3, 4).ToCall(myAd
by sethammons 3y ago
My goto simple example here for dumb mocks is a multiply function.
You are doing it wrong if you have:
MyMock.Mocks(myMultiplyFunc).WithArgs(3, 4).ToCall(myAddFunc).Times(3).WithArg(4).ExpectsResponse(12).
Your multiply func being backed by add is not important to the unit test for multiplying. If you change it to have special handling for bitshifting in different cases, your mock tests break. Badly.
Mocks add coupling. Coupling is brittle. There are other testing options available. Are mocks sometimes useful? Maybe. I've not needed a mock library in 10 years of Go development designing and building robustly tested distributed sytems at scale. Every time I've seen a mock used it is gross. You end up with SomeSAASMock that is auto-generated to provide all the special handling of the real calls to the SAAS.
Don't mock out the full SendGrid API. Make a dependency interface "SendEmail(userID int, content []byte) (SendGridResp, error)". Inject that. Now you just have MyFakeSendGrid structs in tests and you can have it return an error when you want, or not. It is really that simple.
Depending on how detailed your integration or acceptance tests are, you can have sinks that capture network calls and return stubbed data or call the actual service.
- Maxion 3y ago> Make a dependency interface "SendEmail(userID int, content []byte) (SendGridResp, error)". Inject that. Am I an idiot for thinking of this as a Mock? This is how I mock things anyway
- sethammons 3y agoIt is maybe a semantics issue. See https://martinfowler.com/articles/mocksArentStubs.html https://martinfowler.com/articles/mocksArentStubs.html. Traditionally, a mock asserts against internal behavior while a fake does not.
- Maxion 3y agoI think it probably is, it seems like certain parts of the tech community has a more specific language for talking about things, and other parts have a less specific.
- seadan83 3y agoMock vs a fake. The difference lies in where the behavior is specified. In a fake, the behavior is hardcoded or simple. With a mock, it is each test that says what the behavior is. The two scale differently. Mocks are per test, fakes are once.