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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程