Hysen Labs
Framework
gcapizzi/moka avatar
gcapizzi

moka

A Go mocking framework.

50 stars1 forksGoApache-2.0
01
DEEP OPEN-SOURCE ANALYSIS

moka: mocking for Go, Ginkgo first

Moka brings RSpec style readable mocks to Go, wires into Ginkgo with a one line registration, and is built around strict doubles that fail tests loudly.

02
DEEP OPEN-SOURCE ANALYSIS

What makes it different

Moka is a mocking framework for Go, and the README spends real space on how it compares to the other options. The syntax is meant to be readable, inspired by RSpec. Its model naturally supports stubbing different method calls with different arguments and return values without enforcing any order, which avoids coupling your tests to the implementation. Doubles are strict by default, so an unexpected call fails the test instead of silently returning a zero value. Compile time type safety is the tradeoff: the flexible syntax keeps the Go type system out of the picture, and typed doubles catch those mistakes at runtime instead. Argument matchers and enforced order are listed as coming soon.

03
DEEP OPEN-SOURCE ANALYSIS

The Ginkgo hook

Ginkgo is the primary integration. The setup is two lines: import the moka package, then register Ginkgo's Fail function as Moka's double fail handler using RegisterDoublesFailHandler. After that, assertion failures from your doubles land in Ginkgo's reporting. That small registration step is the whole wiring job, and it is the reason Moka works so well in Ginkgo suites.

04
DEEP OPEN-SOURCE ANALYSIS

Using it with plain testing

Support for the standard library testing package has not been added yet, but the README is clear that this does not stop you from using Moka with it. The fail handler type is modelled to match Ginkgo's Fail function, so with the testing package you just supply a doubles fail handler that fails the test. A test double stands in for another object and has to implement the same interface, and strict doubles fail the test on any call that was not allowed or expected.

05
DEEP OPEN-SOURCE ANALYSIS

Editorial conclusion

Moka is a Go mocking framework that pairs especially well with Ginkgo through a fail handler registration, while still working with other frameworks. Support for the standard testing package is planned but not yet present.

06
DEEP OPEN-SOURCE ANALYSIS

Official sources

07
Community notes

Community notes