Unit Testing Code with Hard-To-Mock Dependencies

Unit testing using mocks is a common technique in Go and other languages. However, some dependencies are harder to mock than others; standard library packages such os can be particularly tricky. This talk covers strategies for effectively testing code that depends on such hard-to-mock packages.

Abstract

Unit testing using mocks is a common technique in Go and other languages. However, some dependencies are harder to mock than others; standard library packages such os can be particularly tricky. This talk covers strategies for effectively testing code that depends on such hard-to-mock packages. Mocking dependencies is a common unit testing technique. Mocks are code that can be injected in place of real dependencies, allowing a developer to control the behavior of these dependencies from a test. Mocks may be generated by a tool or written by hand, but either way they make it easy to exercise error conditions and isolate tests from external software and systems. In Go, it’s easiest to use mocks for dependencies that export interfaces. A mock object conforming to some interface can be built or generated and injected for tests, while a real object conforming to the same interface is used in production. However, not all packages provide nice mockable interfaces. In particular, low-level packages such as the os package in the Go standard library tend to export functions (as opposed to structs with methods) and structs with data members (as opposed to accessor functions). These properties make it difficult to build mocks for testing code that depends on such packages.

Video

GoLab is a conference made by Develer.
Develer is a company based in Campi Bisenzio, near Florence. Our motto is : "Technology to give life to your products". We produce hardware and software to create exceptional products and to improve industrial processes and people's well being.
In Develer we have passion for the new technologies and we offer our clients effective solutions that are also efficient, simple and safe for the end users. We also believe in a friendly and welcoming environment where anybody can give their contribution. This passion and this vision are what we've been driven to organize our conference "made by developers for developers".


Subscribe to our newsletter

We hate spam just as much as you do, which is why we promise to only send you relevant communications. We respect your privacy and will never share your information with third parties.
©2024 GoLab | The international conference on Go in Florence-Design & devCantiere Creativo-Made withDatoCMS