dpde7365
2017-03-10 18:00
浏览 15
已采纳

在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?

图片转代码服务由CSDN问答提供 功能建议

在Go中,如何在无需实现每个方法的情况下模拟接口? 假设我有一个 Car 接口和一个实现该接口的 Corolla 结构:

  type Car接口{
  changeTire()
 startEngine()
 .... 
 refuel()
} 
 
type Corolla struct {
 ... 
} 
 
func(c Corolla)changeTire(){。  ..} 
 
func(c花冠)startEngine(){...} 
 
func(c花冠)refuel(){...} 
   
 
 <  p>假设我还有一个 Garage 结构,该结构取决于 Car  
 
 
  type Garage struct {
 MyCar  Car 
} 
 
func(g Garage)PrepareCarToDrive(){
 g.MyCar.changeTire()
 g.MyCar.refuel()
 g.MyCar.startEngine()
} 
   
 
 

并且我想测试 Garage ,所以我创建了一个实现 Car < / p>

  type MockCar结构{
 ... 
} 
 
func(c MockCar)changeTire(){...} 
 
func(c MockCar)startEngine  (){...} 
 
func(c MockCar)refuel(){...} 
   
 
 

现在我已经测试了测试 Pr epareCarToDrive ,我使用 MockCar

  func TestGarage_PrepareCarToDrive_DoesSomething(t * testing.T){
模拟汽车:= MockCar {}  
 garageUnderTest:= Garage {} 
 garageUnderTest.MyCar = mockCar 
 
 //其他设置
 
 //当Garage调用mockCar.changeTire()时,应该执行X 
 ... 
}  
 
func TestGarage_PrepareCarToDrive_DoesSomethingElse(t * testing.T){
模拟汽车:= MockCar {} 
 garageUnderTest:=车库{} 
 garageUnderTest.MyCar =模拟汽车
 
 //其他设置
 //何时 车库调用模拟汽车.changeTire(),应该执行Y 
 ... 
} 
   
 
 

我的问题是,我怎么能拥有 mockCar 每个测试都会做不同的事情吗? 我知道我可以为每个测试创建 Car 的不同模拟实现。 但这会很快消失,因为我向 Car 添加了更多方法。

我来自Java背景,因此我正在寻找一些东西 就像Mockito一样,它将让我仅模拟每次测试所需的方法。

在Go中执行此操作的最佳方法是什么? 还是我缺少更基本的东西?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongli8722 2017-03-10 19:57
    已采纳

    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)

    已采纳该答案
    打赏 评论
  • doute3621 2017-03-10 18:52

    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.

    打赏 评论

相关推荐 更多相似问题