September 2018: testing

Meetup #53. Please RSVP at Meetup.

We get started at 6:30 pm.

Sponsors

Venue provided by Startup Edmonton.

Food sponsored by Bellstone Engineering.

Talks

Unit Testing Code with Hard-to-Mock Dependencies

Adam Wolfe Gordon (DigitalOcean) gave a tour of testing code whose dependencies are hard to fake. He framed mocks broadly as "test doubles" — objects pre-programmed with expectations — used to decouple tests from the real world, avoid running dependencies, and force hard-to-simulate error conditions. He contrasted easy-to-mock dependencies that export interfaces or methods (like net/http) with hard-to-mock ones that expose free functions, return complex structs, or use cgo (like os), then walked through four techniques: isolation, wrapping, package mocking, and combinations of the three. Worked examples included wrapping the os package behind a small OS interface with a realOS implementation, and mocking time.Now/time.Since with his own mockpkg tool. He closed with honest pros and cons for each technique — coverage versus indirection, performance, and code structure.

Key points:

  • Mocks (test doubles) decouple tests from real dependencies and let you force error paths.
  • Hard-to-mock dependencies expose free functions, return complex structs, or use cgo (os).
  • Isolation: factor out dependency-free logic and unit-test that.
  • Wrapping: put the dependency behind a local interface/struct for dependency injection and standard mocking.
  • Package mocking: store free functions in variables and swap in generated mocks (mockpkg).
  • Combine techniques for non-trivial codebases.
  • Each technique trades coverage against indirection, performance, and code structure.

Adam Wolfe Gordon (slides pdf)

News

Events