Suppose that I have a struct Car
and it has some methods I want to test. For example IgniteEngine
, SwitchGear
, and drive
. As you can see that drive
depends on the other methods. I need a way to mock IgniteEngine
and SwitchGear
.
I think I am supposed to use interface but I don't quite understand how to accomplish it.
Suppose Car
is now an interface
type Car interface {
IgniteEngine()
SwitchGear()
Drive()
}
I can create a MockCar
and two mocked functions for IgniteEngine
and SwitchGear
but now how do I test the source code for Drive
?
Do I copy and paste my source code into the mock object? That seems silly. Did I get the idea wrong about how to perform mocking? Does mocking only work when I do dependency injection?
Now what if Drive
actually depends on external library like a database or a message broker system?
Thank you