Skip to content

nhatthm/aferomock

Repository files navigation

Mock for spf13/afero

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

aferomock is a mock library for spf13/afero

Prerequisites

  • Go >= 1.17

Install

go get go.nhat.io/aferomock

Examples

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)
			}
		})
	}
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this