Mongo Aggregate query with project unable to Mock data, need to write test cases for It

Hi, @Rishabh_Soni1,

Welcome to the MongoDB Community Forums. I understand that you’re attempting to mock parts of the MongoDB .NET/C# Driver. A widely accepted testing practice is “Don’t mock what you don’t own”. In other words, don’t mock third-party dependencies, but only your own abstractions. In your example, you should mock AlertGetByStationIdInternalAsync (your code), not IAggregateFluent<TResult> (driver code).

Using this strategy you can integration test AlertGetByStationIdInternalAsync, which would talk to the database and use the actual driver implementation. You can then separately unit test your code that depends on AlertGetByStationIdInternalAsync without talking to a database or setting up hard-to-configure error conditions. I would recommend full system tests that exercise the full stack, but you wouldn’t have to test all the edge cases (such as your database being offline or containing unexpected data or other errors).

I hope that helps in developing your testing strategy.

Sincerely,
James

2 Likes