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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀