Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:05:21+00:00 2026-06-12T17:05:21+00:00

I am trying to do tdd and use mongodb as database. But i cant

  • 0

I am trying to do tdd and use mongodb as database. But i cant resolve problem of mocking mongodb. Is there any ability to mock mongodb for unit testing in .NET?


Update

I found very good soltion reading blog.
You can find it here:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:05:23+00:00Added an answer on June 12, 2026 at 5:05 pm

    Instead of mocking MongoDB, you should be mocking a layer on top of MongoDB.

    You might want to consider an interface that exposes the operations on your repository which are agnostic to the underlying data store. For example, you might want an interface that abstracts out operations on Student types, like so:

    public interface IStudentOperations
    {
        void Add(Student student);
    }
    

    When you create your other dependencies, you inject instances of the above interface, or whichever higher-level abstractions you choose.

    The point is, don’t expose MongoDB directly.

    Once you do that, you can mock the interfaces you create all you want, having one implementation for testing against the mock implementation, and then an actual implementation with it’s own tests to validate that operations on the implementation are correct when the underlying implementation is with MongoDB.

    While it’s definitely possible to mock most of MongoDB’s classes (as the methods are virtual), you gain the benefit of being persistence agnostic; if you want to switch to say, CouchDB or elasticsearch, you don’t have to change the calls to these interfaces, you simply create a new implementation.


    Because you are trying to test the implementation of the repository, then you are generally fine, as has been stated before, most of MongoDB’s functions are virtual, which is friendly to most mocking libraries.

    That said, you’ll have to make sure that you pass the MongoDatabase into your repository (not create it in the repository) so that in your unit tests, you can create the appropriate mock and then pass it into your repository implementation for testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had become totally convinced about TDD and trying to use it by the
I'm getting started with unit testing and trying to do some TDD. I've read
I am trying out TDD and creating fake objects, I want to use XML
I'm trying to do TDD with PyMock, but I keep getting error when I
I'm developing a Spring webflow, trying to use TDD so I've extended AbstractXmlFlowExecutionTests. I
I'm trying to improve my TDD/OO skills and each time I try and use
I am trying to make a case for myself to use TDD and the
I'm trying to get into the unit testing and TDD way of doing things
I am new to TDD and trying to solve a problem. In my task,
I'm trying to move toward TDD, ORM, Mocking, ect. I need a good example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.