Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking single methods in Go

In Go, how can I mock an interface without having to implement every method? Let's say I have a Car interface and a Corolla struct that implements that interface:

type Car interface {
    changeTire()
    startEngine()
    ....
    refuel()
}

type Corolla struct {
    ...
}

func (c Corolla) changeTire() {...}

func (c Corolla) startEngine() {...}

func (c Corolla) refuel() {...}

Let's say I also have a Garage struct that depends on Car:

type Garage struct {
    MyCar Car
}

func (g Garage) PrepareCarToDrive() {
    g.MyCar.changeTire()
    g.MyCar.refuel()
    g.MyCar.startEngine()
}

And I want to test Garage, so I create a MockCar that implements Car

type MockCar struct {
    ...
}

func (c MockCar) changeTire() {...}

func (c MockCar) startEngine() {...}

func (c MockCar) refuel() {...}

Now I have tests that test PrepareCarToDrive and I use the MockCar:

func TestGarage_PrepareCarToDrive_DoesSomething(t *testing.T) {
    mockCar := MockCar{}
    garageUnderTest := Garage{}
    garageUnderTest.MyCar = mockCar

    // some other setup

    // when Garage calls mockCar.changeTire(), should do X
    ...
}

func TestGarage_PrepareCarToDrive_DoesSomethingElse(t *testing.T) {
    mockCar := MockCar{}
    garageUnderTest := Garage{}
    garageUnderTest.MyCar = mockCar

    // some other setup 
    // when Garage calls mockCar.changeTire(), should do Y
    ...
}

My question is, how can I have mockCar do different things each test? I know that I can create a different mock implementation of Car for each test. But this will get out of hand really quickly as I add more methods to Car.

I'm coming from a Java background so I'm looking for something like Mockito that will let me mock just the methods I need for each test.

What is the best way to do this in Go? Or am I missing something more fundamental?

like image 857
tir38 Avatar asked Mar 10 '17 18:03

tir38


People also ask

What's the difference between faking mocking and stubbing?

Fakes are generally used to improve performance by avoiding external calls. Mocks are used to verify the behavior of our code. Stubs are used to provide data that our code needs to run. We should use the simplest test double that will get the job done.


1 Answers

If you embed the interface type itself in your mock struct, you can then only implement the methods you need. For example:

type MockCar struct {
    Car
    ...
}

func (c MockCar) changeTire() {...}

Even though your struct only implements changeTire explicitly, it still satisfies the interface because the Car field provides the rest. This works as long as you don't try to call any of the unimplemented methods (which will cause a panic because Car is nil)

like image 193
Andy Schweig Avatar answered Oct 14 '22 19:10

Andy Schweig