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
- 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 ?