dongmi1663 2018-07-30 15:03
浏览 51
已采纳

创建单元测试以使用内容创建文件

I've the following function which getting a file and write content to it.

func setFile(file *os.File, appStr models.App) {

    file.WriteString("1.0")

    file.WriteString("Created-By: application generation process")
    for _, mod := range appStr.Modules {

        file.WriteString(NEW_LINE)
        file.WriteString(NEW_LINE)
        file.WriteString("Application")
        file.WriteString(NEW_LINE)
        file.WriteString("ApplicationContent")
        file.WriteString(NEW_LINE)
        file.WriteString("ContentType")

    }
}

For that I generate a unit test like following

func Test_setFile(t *testing.T) {


    type args struct {
        file   *os.File
        appStr models.App
    }
    var tests []struct {
        name string
        args args
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            setFile(tt.args.file, tt.args.AppStr)
        })
    }
}

The problem here is that im depending on file, what is better approach to create unit test for this kind of function

  1. run code in the unit test which is creating file update it with this function and then parse it and check the values ? is there a better approach for this kind of function ?
  • 写回答

1条回答 默认 最新

  • douyin6188 2018-07-30 15:06
    关注

    The better approach would be to accept an interface, something like io.Writer. In your real usage you can pass in a *os.File, and in your tests you can pass in something easier to work with like a bytes.Buffer.

    Something like (untested but should get you started):

    func setFile(file io.Writer, appStr models.App) {
        fmt.Fprint(file, "1.0")
    
        fmt.Fprint(file, "Created-By: application generation process")
        for _, mod := range appStr.Modules {
            fmt.Fprint(file, NEW_LINE)
            fmt.Fprint(file, NEW_LINE)
            fmt.Fprint(file, "Application")
            fmt.Fprint(file, NEW_LINE)
            fmt.Fprint(file, "ApplicationContent")
            fmt.Fprint(file, NEW_LINE)
            fmt.Fprint(file, "ContentType")
        }
    }
    
    func Test_setFile(t *testing.T) {
        type args struct {
            appStr models.App
        }
        var tests []struct {
            name string
            args args
            expected []byte
       }
        for _, tt := range tests {
            t.Run(tt.name, func(t *testing.T) {
                b := &bytes.Buffer{}
                setFile(b, tt.args.AppStr)
                if !bytes.Equal(b.Bytes(), tt.expected) {
                    t.Error("somewhat bad happen")
                }
            })
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。