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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:15:34+00:00 2026-06-16T13:15:34+00:00

I’ve just started playing around with Moq in my unit tests, but am having

  • 0

I’ve just started playing around with Moq in my unit tests, but am having an issue where the unit test is passing – and I don’t think it should be.

I have two objects, one that despatches data to a queue and another that implements INotifier which is called if the despatcher fails, they look like this (cut down for brevity):

public class EmailNotifier : INotifier
{
    public void Notify(string message) 
    {
        // sends the notification by email
    }
}

public class Despatcher
{
    public void Despatch(int batchNumber, INotifier failureNotifier)
    {
        try
        {
            if (batchNumber.Equals(0)) throw new InvalidOperationException("Error message");
        }
        catch (InvalidOperationException ex)
        {
            failureNotifier.Notify(ex.ToString());

            throw ex;
        }
    }
}

I am unit testing the Despatcher to specifically verify that Notify is being called on the provided INotifier (which I’m mocking) when it fails (I’m intentionally forcing the Despatcher to fail by passing a 0 batch number). I set up my mocks like this:

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Despatcher_notifies_on_failure()
{
    var mockNotifier = new Mock<EmailNotifier>();
    mockNotifier.Setup(n => n.Notify(It.IsAny<string>())).Verifiable();

    var despatcher = new Despatcher();
    despatcher.Despatch(0, mockNotifier.Object);

    mockNotifier.Verify(n => n.Notify(It.IsAny<string>()), Times.Once());
}

This passes the test fine, which is expected as a 0 batch number raises the exception which causes the INotifier to call Notify (when I step through the test everything works as expected).

So, I go on to comment out the failureNotifier.Notify(ex.ToString()) line and run the test again – passes fine? I’m not sure whether I’m setting up and verifying correctly as I’ve only been using Moq for about 2 hours now but I thought I was understanding this correctly, but this has just thrown me a bit. I expect this test to fail as I specifically want to make sure Notify is called in the event of a failure – can anyone see anything obviously wrong here? Thanks in advance for your help as always.

  • 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-16T13:15:35+00:00Added an answer on June 16, 2026 at 1:15 pm

    Your test never gets to the verification part. Why? This line

    despatcher.Despatch(0, mockNotifier.Object);
    

    Throws exception, which is consumed by ExpectedException attribute and test ends. The mockNotifier.Verify line is never executed.

    What you want is two unit tests, instead:

    • one testing that notifier is called upon exception (with .Verify). Note that you’ll have to wrap .Despatch call into try { } catch { }, so that exception is ignored.
    • second, checking that exception is rethrown (with ExpectedException)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I

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.