Within my class, there are multiple class functions, with one specifically executing a mongoose query. The structure is similar to:
export class ExampleService {
constructor(
@InjectModel(Example.name) private exampleModel: Model<Example>,
@InjectModel(MyMongoose.name) private myMongooseModel: Model <MyMongoose>
){}
//function in focus for unit testing
async addToCollection(name) {
if(name.length < 1) throw new Error ('No name entered')
else {
this.myMongooseModel.query()
}
}
//..other functions that utilize addToCollection()
}
My goal is to test the behavior of addToCollection()
by ensuring it throws an error when name
is too short, and that it initiates a query to myMongooseModel
when the name is sufficient.
Past attempts at mocking ExampleService led to having to mock not only addToCollection but also the query to myMongooseModel. This approach seems incorrect for testing this specific function. How can I solely mock the call to myMongooseModel?