I am new to Golang. I am trying to write unit test cases for the functions that I implemented. Here is one such,
func SaveProjectInfo(projectInfo processor.ProjectInfo, configMap map[string]string, client *mongo.Client) error {
collection := client.Database(configMap[constants.Dbname_str_key]).Collection(configMap[constants.Db_collection_str])
return UpsertProjectInfo(collection, projectInfo)
}
Here, the function takes 3 arguments, out of which first two can be created with stubbed data. I am trying to create a mock mongo client using go.mongodb.org/mongo-driver/mongo/integration/mtest -
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
client := mt.Client
Which looks to be not working, as it ends up creating a nil client.
Also, func UpsertProjectInfo()
that makes a real-time mongo call, is being invoked here internally and it is required to be mocked for the sake of writing unit test case of func SaveProjectInfo()
.
I looked into the Testify mock package from here, https://github.com/stretchr/testify. However, I could not quite figure out how to make use of the testObj(which I am going to set mock expectations to) during the actual function call.
Kindly guide, thank you!