douya1061 2018-11-18 11:19
浏览 52
已采纳

如何在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?

  • 写回答

1条回答 默认 最新

  • duanmao1319 2018-11-18 15:13
    关注

    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:

    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.

    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")
        ...
    }
    

    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
    }
    

    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.

    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.

    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.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么