dpjo15650 2019-07-05 17:49
浏览 41
已采纳

如何仅模拟接口的一种方法

I am struggling to understand mocking in Go (am looking for something related to Mockito.spy equivalent of java in Go).

Let's say I have an interface in Go with 5 methods. But the piece of code that I want to test has references to only two methods. Now how do I mock this dependency without implementing all methods , i.e my actual implementation in source code implements 5 methods of interface , but is there a way to avoid implementing dummy interface implementation of 5 methods in test file. Following is the way am currently doing , implementing 5 methods is manageable but what if the interface has 20 methods , it becomes tedious to mock implement all the methods in test file.

Example:

Source code in handler.go:

type Client struct {}
type ClientStore interface {
  func(c *Client) methodOne() error {// some implementation}
  func(c *Client) methodTwo() error {// some implementation}
  func(c *Client) methodThree() error {// some implementation}
  func(c *Client) methodFour() error {// some implementation}
  func(c *Client) methodFive() error {// some implementation}
}

Source code in api.go:

 func processFeed(c Client) error {
     err := c.methodOne()
     if(err != null) {
      return err
    }
     err1 := c.methodTwo()
     if(err1 != null) {
      return err1
    }
 }

Test class code:

import "testify/mock"

func TestFeed(t *testing.T){
   mockClient := &MockClient{}
   err := processFeed(mockClient)
   assert.NotNil(t , err)

}

type MockClient struct {
  mock.Mock
}

  func(c *MockClient ) methodOne() error {fmt.Printf("methodOne");nil}
  func(c *MockClient ) methodTwo() error {return errors.New("mocked error")}
  func(c *MockClient ) methodThree() error {fmt.Printf("methodThree");nil}
  func(c *MockClient ) methodFour() error {fmt.Printf("methodFour");nil}
  func(c *MockClient ) methodFive() error {fmt.Printf("methodFive");nil}


Question:

Is there a way to mock only what i required in the above case only the methodOne() and methodTwo() methods and not worry about remaining methods in tests ? Can you please suggest any other alternatives if they are present ? Thank you

  • 写回答

1条回答 默认 最新

  • dtoka218420 2019-07-05 18:07
    关注

    First, if your interface has 5 methods, and you're only using one, your interface is too big. Use a smaller interface.

    type BigInterface interface {
        Thing1()
        Thing2()
        ThingN()
    }
    
    type SmallInterface interface {
        Thing1()
    }
    
    func MyFunc(i SmallInterface) { ... }
    

    Another option is to create a complete implementation of the full interface, by embedding the full interface. This will panic if you try to access one of the other methods, but will work for testing if you're careful. (But please don't do this in production code!)

    type BigInterface interface {
        Thing1()
        Thing2()
        ThingN()
    }
    
    type myImplementation struct {
        BigInterface
    }
    
    func (i *myImplementation) Thing1() { ... }
    

    Now myImplementation satisfies the BigInterface interface, by virtue of containing an embedded instance of BigInterface. If you never set that embedded instance to anything, then calling those methods will panic, but you can still define Thing1 to do what you want for your tests.

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

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏