如何在Golang中实现存根?存根和模拟之间有什么区别? [英] How to implement stub in Golang? And what difference between stub and mock?

查看:50
本文介绍了如何在Golang中实现存根?存根和模拟之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Golang的单元测试中使用了模拟.但是如何在Golang的实现代码中获得存根和模拟之间的区别?

I am using mocks in unit testing in Golang. But how to get the difference between stub and mock in the implementation code in Golang?

推荐答案

GO中模拟和存根的意图与不同的编程语言相同:

Intention of mocks and stubs in GO is the same as different programming languages:

  • 存根替代了代码中将在测试执行期间使用的某些依赖性.它通常是为一种特定的测试而构建的,不太可能再用于另一项测试,因为它具有硬编码的期望和假设.
  • 模拟将存根提高到一个新的水平.它增加了配置手段,因此您可以为不同的测试设置不同的期望.这使模拟更加复杂,但可用于不同的测试.
  • stub is replacement for some dependency in your code that will be used during test execution. It is typically built for one particular test and unlikely can be reused for another because it has hardcoded expectations and assumptions.
  • mock takes stubs to next level. It adds means for configuration, so you can set up different expectations for different tests. That makes mocks more complicated, but reusable for different tests.

让我们检查一下它在示例中如何工作:

Let's check how that works on example:

在我们的例子中,我们拥有http处理程序,该处理程序在内部对另一个Web服务进行http调用.为了测试处理程序,我们希望将处理程序代码与不受控制的依赖区分开(外部Web服务).我们可以使用 stub mock 来做到这一点.

In our case, we have http handler that internally makes http calls to another web service. To test handler we want to isolate handler code from dependency we do not control (external web service). We can do that by either using stub or mock.

我们的处理程序代码与 stub mock 相同.我们应该注入 http.Client 依赖项,以便能够在单元测试中将其隔离:

Our handler code is the same for stub and mock. We should inject http.Client dependency to be be able to isolate it in unit test:

func New(client http.Client) http.Handler {
    return &handler{
        client: client,
    }
}

type handler struct {
    client http.Client
}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    ...
    // work with external web service that cannot be executed in unit test
    resp, err := h.client.Get("http://example.com")
    ...
}

我们对运行时 http.Client 的替换在 stub 中是直接的:

Our replacement for run-time http.Client is straight-forward in stub:

func TestHandlerStub(t *testing.T) {
    mux := http.NewServeMux()
    mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // here you can put assertions for request

        // generate response
        w.WriteHeader(http.StatusOK)
    }))
    server := httptest.NewServer(mux)

    r, _ := http.NewRequest(http.MethodGet, "https://some.com", nil)
    w := httptest.NewRecorder()
    sut := New(server.Client())
    sut.ServeHTTP(w, r)
    //assert handler response
}

模拟故事更加复杂.我正在跳过模拟实现的代码,但这是其接口的外观:

Mock story is more complicated. I am skipping code for mock implementation but this is how its interface may look like:

type Mock interface {
    AddExpectation(path string, handler http.HandlerFunc)
    Build() *http.Client
}

这是使用Mock进行测试的代码:

This is code for test using Mock:

func TestHandlerMock(t *testing.T) {
    mock := NewMock()
    mock.AddExpectation("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // here you can put assertions for request

        // generate response
        w.WriteHeader(http.StatusOK)
    }))

    r, _ := http.NewRequest(http.MethodGet, "https://some.com", nil)
    w := httptest.NewRecorder()
    sut := New(mock.Build())
    sut.ServeHTTP(w, r)
    //assert handler response
}

对于这个简单的示例,它没有太大的价值.但是请考虑更复杂的情况.您可以构建更简洁的测试代码,并以更少的行覆盖更多案例.

For this simple sample it adds no much value. But think about more complicated cases. You can build much cleaner tests code and cover more cases with less lines.

如果我们必须调用2个服务并稍微模拟一下,这就是测试设置的样子:

This is how tests setup may look like if we have to call 2 services and evolved our mock a little bit:

mock.AddExpectation("/first", firstSuccesfullHandler).AddExpectation("/second", secondSuccesfullHandler)
mock.AddExpectation("/first", firstReturnErrorHandler).AddExpectation("/second", secondShouldNotBeCalled)
mock.AddExpectation("/first", firstReturnBusy).AddExpectation("/first", firstSuccesfullHandler)AddExpectation("/second", secondSuccesfullHandler)

您可以想象,如果我们没有小型的模拟助手,则必须在测试中复制粘贴处理程序逻辑多少次.复制粘贴的代码使我们的测试变得脆弱.

You can imagine how many times you have to copy-paste handler logic in tests if we do not have our tiny mock helper. That copy-pasted code makes our tests fridgile.

但是构建自己的模拟并不是唯一的选择.您可以依赖现有的模拟SQL的模拟程序包,例如 DATA-DOG/go-sqlmock .

But building your own mocks is not the only options. You can rely on existing mocking packages like DATA-DOG/go-sqlmock that mocks SQL.

这篇关于如何在Golang中实现存根?存根和模拟之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆