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 265653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:47:20+00:00 2026-05-11T22:47:20+00:00

I use NMock2, and I’ve drafted the following NMock classes to represent some common

  • 0

I use NMock2, and I’ve drafted the following NMock classes to represent some common mock framework concepts:

  • Expect: this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call to VerifyAllExpectationsHaveBeenMet()).

  • Stub: this specifies what a mocked method should return but cannot cause a test to fail.

So which should I do when?

  • 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-05-11T22:47:20+00:00Added an answer on May 11, 2026 at 10:47 pm

    A lot of mocking frameworks are bringing the concepts of mocks & stubs closer & closer together to the point that they can be considered functionally almost the same. From a conceptual perspective however, I usually try to follow this convention:

    • Mock: Only when you are explicitly trying to verify the behaviour of the object under test (i.e. your test is saying that this object must call that object).
    • Stub: When you are trying to test some functionality/behaviour, but in order to get that working you need to rely on some external objects (i.e. your test is saying that this object must do something, but as a side effect, it may call that object)

    This becomes clearer when you make sure that each of your unit tests only test one thing. Sure if you try to test everything in one test then you might as well Expect everything. But by only expecting the things that specific unit test is checking for, your code is much clearer because you can see at a glance what the purpose of the test is.

    Another benefit of this is that you’ll be slightly more insulated from change & get better error messages when a change causes a break. In other words if you subtley change some part of your implementation, your more likely to get only one test case breaking, which will show you exactly what’s broken, rather than a whole suite of tests breaking & just creating noise.

    Edit: It might be clearer based on a contrived example where a calculator object audits all additions to a database (in pseudo-code)…

    public void CalculateShouldAddTwoNumbersCorrectly() {
        var auditDB = //Get mock object of Audit DB
        //Stub out the audit functionality...
        var calculator = new Calculator(auditDB);
        int result = calculator.Add(1, 2);
        //assert that result is 3
    }
    
    public void CalculateShouldAuditAddsToTheDatabase() {
        var auditDB = //Get mock object of Audit DB
        //Expect the audit functionality...
        var calculator = new Calculator(auditDB);
        int result = calculator.Add(1, 2);
        //verify that the audit was performed.
    }
    

    So in the first test case we’re testing the functionality of the Add method & don’t care whether an audit event occurs or not, but we happen to know that the calculator won’t work with out an auditDB reference so we just stub it out to give us the minimum of functionality to get our specific test case working. In the second test we’re specifically testing that when you do an Add, the audit event happens, so here we use expectations (notice that we don’t even care what the result is, since that’s not what we’re testing).

    Yes you could combine the two cases into one, & do expectations and assert that your result is 3, but then you’re testing two cases in one unit test. This would make your tests more brittle (since there’s a larger surface area of things that could change to break the test) and less clear (since when the merged test fails its not immediately obvious what the problem is.. is the addition not working, or is the audit not working?)

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

Sidebar

Related Questions

I want to start using mock objects on my c# project. After a quick
I hope this question is not 'controversial' - I'm just basically asking - has
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
I need to write a unit tests for a class that reads a xml
I have a quick question that I could not figure out in the docs
I have a .NET application with a web front-end, WCF Windows service back-end. The
I have to work with an API that uses a lot of by-reference parameters.
I've got an app which is using a WCF service. Now I'd like to
First let me state that, despite being a fairly new practitioner of TDD, I'm

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.