drnycqxwz63508434 2018-01-03 18:08
浏览 48
已采纳

测试和模拟不返回任何值的函数

I want to test a function, which does not return any value, but instead triggers other functions. While reading about testing, I found information that this is called a behavioral verification and that by mocking I can check what functions and in what order are triggered. However, I have a problem to implement the proper mocking technique for my code.

Let's consider a simple example of the following interface and struct (the example is very basic just to make it easier to explain):

type ExampleInterface interface {
    DoSomething(arg int)
    DoEvenMore(arg int)
    AndEvenMore(arg int)
} 

type ExampleStruct struct {
     Id string 
     // Other fields
}

func (e *ExampleStruct) DoSomething(arg int) {
     arg2 := arg * 2
     e.DoEvenMore(arg2)
     arg3 := arg * 3
     e.AndEvenMore(arg3)  
}

func (e *ExampleStruct) DoEvenMore(arg int){
     fmt.Println("I did so many operations here using ", arg)
}

func (e *ExampleStruct) AndEvenMore(arg int) {
     // Performing other operations on arg
}

Now, I want to test function DoSomething. Since it does not return any value what I want to do is to test whether after calling this function with argument 3 the following chain of events happens: function DoEvenMore is called once with argument 6, and next function AndEvenMore is called once with argument 9

I wrote the following mocking test:

func TestDoSomething(t *testing.T) {

    mockCtrl := gomock.NewController(t)
    defer mockCtrl.Finish()

    mockClient := mocks.NewMockExampleInterface(mockCtrl)

    example := ExampleStruct("ExampleId")

    gomock.InOrder(
        mockClient.EXPECT().DoSomething(3).Return().Times(1)
        mockClient.EXPECT().DoEvenMore(6).Return().Times(1)
        mockClient.EXPECT().AndEvenMore(9).Return().Times(1) 
    )

    example.DoSomething(3)
}

However, when I run this test I get the error: Unexpected call to *mocks.MockExampleInterface.DoSomething(..).

How I should properly perform the mocking in such example?

  • 写回答

1条回答 默认 最新

  • dongmouhao7438 2018-01-03 19:43
    关注

    The two methods called by DoSomething are concrete implementations, you cannot mock something like that in Go.

    To achieve what you want DoSomething would have to depend on an interface that defines those two methods and call those instead of the concrete ones.

    type DoMorer interface {
        DoEvenMore(arg int)
        AndEvenMore(arg int)
    } 
    
    type ExampleStruct struct {
         Id string 
         // Other fields
    }
    
    func (e *ExampleStruct) DoSomething(arg int, dm DoMorer) {
         arg2 := arg * 2
         dm.DoEvenMore(arg2)
         arg3 := arg * 3
         dm.AndEvenMore(arg3)  
    }
    

    With a setup like this, during testing, you can create a mock for the DoMorer interface and pass that to DoSomething.


    DoMorer doesn't have to be passed to as an argument, it could be a field of ExampleStruct or a global, whatever suits your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题