I got an example from @volker about table driven test like following
But currently I miss what I should put in the real test, this test is using byte, currently im not sure what to put in the args
and the expected []byte
,
e.g. I want to check that in the file there is 2 new line
and then application
entry, how can I do it without the need to create the real file and parse it?
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, "
")
fmt.Fprint(file, "
")
fmt.Fprint(file, appStr.vtype) //"userApp"
fmt.Fprint(file, "
")
fmt.Fprint(file, appStr.name) //"applicationValue"
fmt.Fprint(file, "
")
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")
}
})
}
}
I read and understand the following example but not for byte and file https://medium.com/@virup/how-to-write-concise-tests-table-driven-tests-ed672c502ae4