dongruyan4948 2014-01-04 17:03
浏览 348
已采纳

如何在go中模拟对ioutil.ReadFile的调用?

I have the following function:

func ObtainTranslationStringsFile(path string) ([]string, error) {
    if contents, err := ioutil.ReadFile(path); err != nil {
        return ObtainTranslationStrings(string(contents))
    } else {
        return nil, err
    }
}

I need to mock ioutil.ReadFile, but I'm not sure how to do it. Is it possible?

  • 写回答

3条回答 默认 最新

  • dongqing4070 2014-01-04 18:05
    关注

    Don't mock ioutil.ReadFile. Once you mocked it, testing ObtainTranslationStringsFile wouldn't test any more than testing ObtainTranslationStrings would. If you need to test ObtainTranslationStringsFile, create a temporary file and pass its path to ObtainTranslationStringsFile. That way you will actually test the functionality that is added by that function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?