Mocking single methods in Go

huangapple go评论88阅读模式
英文:

Mocking single methods in Go

问题

在Go语言中,你可以使用第三方库来模拟接口而不必实现每个方法。一个常用的库是testify/mock。下面是使用该库来模拟接口的示例:

首先,你需要在项目中导入testify/mock库:

import (
	"github.com/stretchr/testify/mock"
)

然后,你可以创建一个MockCar结构体,它继承自mock.Mock并实现Car接口的方法:

type MockCar struct {
	mock.Mock
}

func (m *MockCar) changeTire() {
	m.Called()
}

func (m *MockCar) startEngine() {
	m.Called()
}

func (m *MockCar) refuel() {
	m.Called()
}

在测试中,你可以使用On方法来设置每个方法的期望行为,并使用ReturnReturnXXX方法来指定返回值。下面是一个示例:

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

	// 设置changeTire方法的期望行为
	mockCar.On("changeTire").Return()

	// 设置refuel方法的期望行为
	mockCar.On("refuel").Return()

	// 设置startEngine方法的期望行为
	mockCar.On("startEngine").Return()

	// 执行被测试的函数
	garageUnderTest.PrepareCarToDrive()

	// 断言方法是否按预期被调用
	mockCar.AssertExpectations(t)
}

使用On方法可以为每个方法设置不同的期望行为,从而实现在每个测试中mockCar执行不同的操作。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

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?

答案1

得分: 7

如果你在模拟结构体中嵌入接口类型本身,那么你只需要实现你需要的方法。例如:

type MockCar struct {
    Car
    ...
}

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

尽管你的结构体只显式实现了changeTire方法,但它仍然满足接口,因为Car字段提供了其余的方法。只要你不尝试调用任何未实现的方法(这会导致恐慌,因为Carnil),这种方式就可以工作。

英文:

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)

答案2

得分: 4

最简单的方法是使用一些基本实现作为测试结构的嵌入,并仅覆盖你要测试的方法。以下是使用你的类型的示例:

type MockCar struct {
    Corolla // 嵌入类型,因此 Corolla 的方法实现会被提升
}

// 覆盖 Corolla 的实现
func (c MockCar) changeTire() {
    // 测试内容
}

// refuel() 和 startEngine(),由于没有被覆盖,会使用 Corolla 的实现

另一种选择是,如果你需要每个测试都有不同的实现,可以使用带有函数字段的模拟对象:

type MockCar struct {
    changeTireFunc func()
    startEngineFunc func()
    ....
    refuelFunc func()
}

func (c MockCar) changeTire() {
    if c.changeTireFunc != nil {
        c.changeTireFunc()
    }
}

func (c MockCar) startEngine() {
    if c.startEngineFunc != nil {
        c.startEngineFunc()
    }
}

func (c MockCar) refuel() {
    if c.refuelFunc != nil {
        c.refuelFunc()
    }
}

// 测试代码

func TestGarage_PrepareCarToDrive_DoesSomething(t *testing.T) {
    // 假设我们需要 refuel(),但默认实现就可以
    // changeTire() 需要一个模拟的测试实现
    // 而我们根本不需要 startEngine()
    mockCar := MockCar{
        changeTireFunc: func() {
            // 测试功能
        },
        refuelFunc: Corolla.refuel,
    }
    garageUnderTest := Garage{}
    garageUnderTest.MyCar = mockCar

    // 其他设置

    // 当 Garage 调用 mockCar.changeTire() 时,应该做 X
    ...
}

当你尝试使用另一个类型的方法作为默认实现时,这种风格可能不太有用,但如果你有一些独立的函数可以作为默认或测试实现,或者对于你没有专门模拟的函数可以接受一个简单的返回值(例如上面示例中模拟的 startEngine() 的行为,当调用时什么都不做,因为 startEngineFunc 字段为 nil),这种风格非常有用。

如果你愿意,你还可以将默认实现(例如调用 (Corolla{}).startEngine())嵌入到模拟方法中,如果相关的函数字段为 nil,则使用默认实现。这样可以同时拥有默认的非平凡实现和通过更改相关函数字段在模拟中随意切换实现的能力。

英文:

The easiest method is to use some base implementation as an embed for your test structure, and only override the method you're testing. Example using your types:

type MockCar struct {
    Corolla // embedded, so the method implementations of Corolla get promoted
}

// overrides the Corolla implementation
func (c MockCar) changeTire() {
    // test stuff
}

// refuel() and startEngine(), since they are not overridden, use Corolla's implementation

https://play.golang.org/p/q3_L1jf4hk


An alternative, if you need a different implementation per test, is to use a mock with function fields:

type MockCar struct {
    changeTireFunc func()
    startEngineFunc func()
    ....
    refuelFunc func()
}

func (c MockCar) changeTire() {
    if c.changeTireFunc != nil {
        c.changeTireFunc()
    }
}

func (c MockCar) startEngine() {
    if c.startEngineFunc != nil {
        c.startEngineFunc()
    }
}

func (c MockCar) refuel() {
    if c.refuelFunc != nil {
        c.refuelFunc()
    }
}

// test code

func TestGarage_PrepareCarToDrive_DoesSomething(t *testing.T) {
    // let's say we require refuel(), but the default implementation is fine
    // changeTire(), however, requires a mocked testing implementation
    // and we don't need startEngine() at all
    mockCar := MockCar{
        changeTireFunc: func() {
            // test functionality
        },
        refuelFunc: Corolla.refuel,
    }
    garageUnderTest := Garage{}
    garageUnderTest.MyCar = mockCar

    // some other setup

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

https://play.golang.org/p/lf7ny-lUCS

This style is a bit less useful when you're trying to use another type's methods as the default implementation, but can be very useful if you have some stand-alone functions that can be used as your default or test implementation, or if a trivial return is acceptable for functions you haven't specifically mocked (like the behavior of the mocked startEngine() in the example above, which does nothing at all when called because the startEngineFunc field is nil).

You can also, if you wish, bake the default implementation (like a call to (Corolla{}).startEngine()) into the mock method if the relevant function field is nil. This allows you the best of both worlds, with a default non-trivial implementation and the ability to hotswap out implementations at will on the mock just by changing the relevant function field.

huangapple
  • 本文由 发表于 2017年3月11日 02:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/42724913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定