I'm using https://github.com/vektra/mockery to generate my mock files.
Assume I have this test for function GetQualifyingListOfBooks
. The signature for the (mocked) client method GetListOfBooks
is:
func GetListOfBooks(ctx context.Context, in *GetListOfBooksRequest, opts ...grpc.CallOption) (*GetListOfBooksResponse, error)
Goal: I want to mock the situation where GetListOfBooks
returns an error, so I've written the following:
package whatever
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
mockClients "github.com/MyOrg/MyApp/internal/app/myservice/mocks"
)
func TestGetQualifyingListOfBooks (t *testing.T) {
client := &mockClients.BookClient{}
client.On("GetListOfBooks", mock.Anything, mock.Anything).Return(nil, mock.AnythingOfType("error"))
realInstance.bookClient = client
b, err := GetQualifyingListOfBooks(realInstance, 10) // this calls `GetListOfBooks`
assert.Nil(t, b)
assert.NotNil(t, err)
}
but it panics with this:
--- FAIL: TestGetQualifyingListOfBooks (0.00s)
panic: assert: arguments: Error(1) failed because object wasn't correct type: Error(1) [recovered]
panic: assert: arguments: Error(1) failed because object wasn't correct type: Error(1)
How do you mock a return value of an error?
Aucun commentaire:
Enregistrer un commentaire