ds3464 2019-09-03 01:18
浏览 79

如何在另一个包中为方法编码模拟

I'm setting service_test.go for service.go.
In service.go, method is called from dao.go.
So I need to mock this dao method.
But I'm not sure how to code this mock method.

Here is the repository structure.

article
  ├ client
  ├ api
  │  ├ main.go
  │  ├ contoroller
  │  │    └ contoroller.go
  │  ├ service
  │  │    └ service.go
  │  ├ dao
  │  │    └ dao.go
  │  ├ go.mod 
  │  ├ go.sum
  │  └ Dockerfile
  ├ nginx
  └ docker-compose.yml

service.go

func GetArticleService(db *sql.DB) []util.Article {
    var articles []util.Article

    // I want to mock dao.GetArticleDao(db) 
    results := dao.GetArticleDao(db)

    article := util.Article{}
    for results.Next() {
        err := results.Scan(&article.ID, &article.UUID, &article.TITLE, &article.CONTENT)
        if err != nil {
            panic(err.Error())
        } else {
            articles = append(articles, article)
        }
    }
    return articles
}

service_test.go

func TestGetArticleService(t *testing.T) {
    //mock db
    db, _, err := sqlmock.New()

    if err != nil {
        t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    }

    defer db.Close()

    //make expected value
    var expectedArticles []util.Article

    expectedArticle1 := util.Article{
        ID:      1,
        UUID:    "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c",
        TITLE:   "test",
        CONTENT: "test",
    }
    expectedArticles = append(expectedArticles, expectedArticle1)

    expectedArticle2 := util.Article{
        ID:      2,
        UUID:    "844bc620-7336-41a3-9cb4-552a0024ff1c",
        TITLE:   "test2",
        CONTENT: "test2",
    }
    expectedArticles = append(expectedArticles, expectedArticle2)

    //check whether expected value and GetArticleService(db) is equal.
    //Since GetArticleService(db) is executed, I want to mock dao.GetArticleDao(db)
    assert.Equal(t, expectedArticles, GetArticleService(db))
}

I want to mock dao.GetArticleDao(db) in service.go to pass test in service_test.go.
But I'm not sure how to mock this code.

  • 写回答

1条回答 默认 最新

  • douhuang2282 2019-09-03 07:03
    关注

    So to do this, you need to be a little more abstract with you definition of the actual code, this allows you to replace the call to a third party module for testing.

    Something like below will allow you to swap out the dao function for a testing mock function that has the same signature. As soon as you define a hard type or function call you tie yourself that function/type permanently.

    func GetArticleService(getArticleDao func(*sql.DB) (string), db *sql.DB) []util.Article {
        var articles []util.Article
    
        // this is the mock, at this point you can pass in a mock function for testing
        results := getArticleDao(db)
    
        article := util.Article{}
        for results.Next() {
            err := results.Scan(&article.ID, &article.UUID, &article.TITLE, &article.CONTENT)
            if err != nil {
                panic(err.Error())
            } else {
                articles = append(articles, article)
            }
        }
        return articles
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真