Is it possible to assert that a real method is called via spying in Go like Java Mockito?
In a word: No.
But what about alternatives?
What you should be testing for is the effects of your function. This is true in any language, not just Go. This is also true whether you're doing unit testing or some higher-level type of testing, including end-to-end testing.
If your methodB()
calls methodA()
, it's not useful to check, per se, that methodA()
is called. What is useful is to check that methodB()
does what you expect. Whether it uses methodA()
to accomplish that or not, is an implementation detail that your test, by definition, should not care about.
Some examples:
Suppose you're writing an integration test, and methodA()
inserts something into the database. Don't assert that methodA()
is called--instead assert that the row you expect is now in the database.
Suppose you're writing a unit test for a Fibonacci generator. Don't assert whether it calls methodA()
, assert whether it returns the correct value.
When you assert that a sub-method is called, you're worried about implementation details--the "how" of your function. That's not what good tests do. Good tests assure that your method produces the correct result. It shouldn't matter, from a testing standpoint, whether it does that by calling methodA()
, methodZ()
, a REST API, or using Jedi powers.
So TL;DR; No, you can't do what you're asking, but that's okay because you shouldn't do what you're asking. There are better alternatives (even in Java).