我從@volker得到了一個有關表驅動測試的示例,如下所示,但是目前我錯過了我應該在真實測試中放入的內容,該測試使用的是字節,目前我不確定要在args和中放入什么expected []byte,例如,我想檢查一下在文件中存在2 new line然后application輸入,我該如何做而無需創建真實文件并對其進行解析?type Models struct { name string vtype string contentType string}func setFile(file io.Writer, appStr Models) { fmt.Fprint(file, "1.0") fmt.Fprint(file, "Created-By: application generation process") for _, mod := range appStr.Modules { fmt.Fprint(file, "\n") fmt.Fprint(file, "\n") fmt.Fprint(file, appStr.vtype) //"userApp" fmt.Fprint(file, "\n") fmt.Fprint(file, appStr.name) //"applicationValue" fmt.Fprint(file, "\n") fmt.Fprint(file, appStr.contentType)//"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") } }) }}我閱讀并理解了以下示例,但不了解字節和文件 https://medium.com/@virup/how-to-write-concise-tests-table-driven-tests-ed672c502ae4
- 1 回答
- 0 關注
- 246 瀏覽
添加回答
舉報
0/150
提交
取消