skip to content
Alvin Lucillo

MongoDB mocked response with ErrNoDocuments

/ 1 min read

💻 Tech

Simulating mongo.ErrNoDocuments with mtest is easy; just don’t return any documents.

mt.Run("test", func(t *mtest.T) {

    t.AddMockResponses(mtest.CreateCursorResponse(1, "test.users", mtest.FirstBatch)) // -- results to mongo.ErrNoDocuments
    // t.AddMockResponses(mtest.CreateCursorResponse(1, "test.users", mtest.FirstBatch, bson.D{{Key: "name": Value: "user1"}})) // -- successful response

    findDocument(t.Client)

})