aferomock is a mock library for spf13/afero
Go >= 1.17
go get go.nhat.io/aferomock
package mypackage_test
import (
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
"go.nhat.io/aferomock"
)
func TestMyPackage(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
mockFs aferomock.FsMocker
expectedError string
}{
{
scenario: "no error",
mockFs: aferomock.MockFs(func(fs *aferomock.Fs) {
fs.MkdirAll("highway/to/hell", os.ModePerm).Return(nil)
}),
},
{
scenario: "error",
mockFs: aferomock.MockFs(func(fs *aferomock.Fs) {
fs.MkdirAll("highway/to/hell", os.ModePerm).Return(errors.New("mkdir error"))
}),
expectedError: "mkdir error",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
err := tc.mockFs(t).MkdirAll("highway/to/hell")
if tc.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedError)
}
})
}
}
If this project help you reduce time to develop, you can give me a cup of coffee :)
or scan this