You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide convenience methods for calls happening only once or none. This is frequent pattern in assertions and would make testing more readable.
Details
Presently, gomock includes func (*Call) Times(int) to handle exactly once or none. There are several reasons why this is an antipattern.
First, it enforces author intention. Changing Times(0) to Times(1) requires only changing the parameter. This a very similar argument to == vs =, which causes issues to this day. Looking at something like Once or None is very helpful for reviewers of code, since they can see clearly the test author intended it to do.
Second, it is easy to get lost in the context when scanning over a complicated test.
Here it is important to know that baz is called exactly twice, but the repetitive calls on foo and fizzBuzz make this less emphasized. When using Once() or None(), the intention of the author is very clear.
Third, it makes the line long: Times(n) is eight characters. Having fewer characters is helpful when working with inlining parameters. Once() And None() are six characters.
Summary
Provide convenience methods for calls happening only once or none. This is frequent pattern in assertions and would make testing more readable.
Details
Presently, gomock includes
func (*Call) Times(int)
to handle exactly once or none. There are several reasons why this is an antipattern.First, it enforces author intention. Changing
Times(0)
toTimes(1)
requires only changing the parameter. This a very similar argument to==
vs=
, which causes issues to this day. Looking at something likeOnce
orNone
is very helpful for reviewers of code, since they can see clearly the test author intended it to do.Second, it is easy to get lost in the context when scanning over a complicated test.
For example:
Here it is important to know that
baz
is called exactly twice, but the repetitive calls onfoo
andfizzBuzz
make this less emphasized. When usingOnce()
orNone()
, the intention of the author is very clear.Third, it makes the line long:
Times(n)
is eight characters. Having fewer characters is helpful when working with inlining parameters.Once()
AndNone()
are six characters.Proposed solution
becomes
The text was updated successfully, but these errors were encountered: