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条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题