Open
Conversation
Member
|
私有变量或者方法,有办法 mock吗? |
Author
你提到的私有变量这个,我正在实现。这个相对来说很简单。 这个需要安装xgo,但是xgo本身是个简单的代码预处理器。最终还是依赖你系统上的go进行编译。在CI环境中跑单测的话,可以使用curl从github下载,github内网应该是很快的。 我不明白你说的集成测试和单测分开是什么意思。是不是指集成测试完全不做mock,单测完全mock?如果是那样的话,我建议集成测试写在一个单独的 |
Author
|
另外,上面提到xgo的API更加简单,还体现在 |
Author
|
上面提到xgo只是一个源码预处理器,这个可以参考我写的原理分析: https://blog.xhd2015.xyz/posts/xgo-monkey-patching-in-go-using-toolexec/ (目前还没有来得及写中文版,可以先将就看着) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
在这个PR中,我使用xgo替换了gomonkey。
xgo相对于gomonkey具有如下优点:
defer patch.Reset()等代码c.Method的形式,而不需要通过字符串指定方法名,更加健壮xgo的项目地址: https://github.com/xhd2015/xgo
使用xgo进行单测的步骤:
注: 由于我只替换了一部分用例,所以这里只运行受影响的那一部分函数。如果仓库作者觉得可行,我可以将它们全部替换掉
测试结果:
在这个PR中我只对部分函数进行了替换,主要是为了提升单测的可维护性。如果这个PR被认为可接受的话,我可以将所有剩余的mock全部替换为xgo,方便后面进行维护。