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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效