dongpai1942 2018-08-11 11:18
浏览 57
已采纳

在Golang中模拟外部依赖项

I have a program in go that connects to AWS S3 and gets a file. I'd like to write some tests for it, but I'd like to know, more generally, how to do these mocks in Golang. I know there are some libraries to create mocks but if I remember correctly I read someone suggesting using only standard libraries for unit tests was the best way to go.

So, how would you test a function like this?

func (s S3Input) Sample(key string) ([]byte, error) {
    var buf []byte
    waBuf := aws.NewWriteAtBuffer(buf)

    _, err := s.Downloader.Download(
        waBuf,
        &s3.GetObjectInput{
            Bucket: aws.String(s.Bucket),
            Key:    aws.String(key),
        },
    )
    if err != nil {
        return nil, err
    }

    return buf, nil
}

Thank you!

  • 写回答

1条回答 默认 最新

  • doutuo2829 2018-08-11 11:40
    关注

    One way to do it is to inject the dependencies in your structure, like such:

    type S3Inputer interface {
        NewWriteAtBuffer(buf []byte) *aws.WriteAtBuffer
        String(v string) *string
    }
    
    type S3Input struct {
        newWriteAtBufferFunc func(buf []byte) *aws.WriteAtBuffer
        stringFunc           func(v string) *string
    }
    
    func (s *S3Input) NewWriteAtBuffer(buf []byte) *WriteAtBuffer {
        return s.newWriteAtBufferFunc(buf)
    }
    
    func (s *S3Input) String(v string) *string {
        return s.stringFunc(v)
    }
    
    func (s S3Input) Sample(key string) ([]byte, error) {
        var buf []byte
        waBuf := s.NewWriteAtBuffer(buf)
    
        _, err := s.Downloader.Download(
            waBuf,
            &s3.GetObjectInput{
                Bucket: s.String(s.Bucket),
                Key:    s.String(key),
            },
        )
        if err != nil {
            return nil, err
        }
    
        return buf, nil
    }
    
    func main() {
        s := &S3Input{
            StringFunc:           aws.String,
            NewWriteAtBufferFunc: aws.NewWriteAtBuffer,
        }
    
        // ...
    }
    

    This allows you to replace those functions with whatever you want for testing, without the need of any testing framework.

    Then, the testing function would look something like this:

    func (s S3Input) TestSample(t *testing.T) {
        s3Mock := &S3Input{
            StringFunc:           (func (v string) *string {
                return nil
            }),
            NewWriteAtBufferFunc: (func (buf []byte) *aws.WriteAtBuffer {
                return nil
            }),
        }
    
        res, err := s3Mock.Sample(...) //
        // asserts & error checks
    }
    

    You could improve it by creating a S3InputMock type instead of reusing the base one, both would implement the S3Inputer interface and your mock could have attributes allowing it to help you with testing. For example, it could count the number of times a function is called, store the arguments it received, have its methods behave differently depending on the attributes you set for easier testing, etc.

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

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办