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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:22:28+00:00 2026-05-12T12:22:28+00:00

This seems like something simple but I can’t seem to get it to work.

  • 0

This seems like something simple but I can’t seem to get it to work.

I have a class with a Save method that simply calls another method ShouldBeCalled(). I want to verify that if I call Save() that the other method ShouldBeCalled() is executed at least once. I thought that I could do the following.

public class ClassA
{
    public virtual void Save()
    {
        ShouldBeCalled();
    }

    public virtual void ShouldBeCalled()
    {
        //This should get executed
    }
}

[TestFixture]
public class ClassA_Test
{
    [Test]
    public void Save_Should_Call_ShouldBeCalled()
    {
        var mockClassA = new Mock<ClassA>();
        mockClassA.Object.Save();

        mockClassA.Verify(x => x.ShouldBeCalled(), Times.AtLeastOnce());
    }
}

But I get the exception “Expected invocation on the mock at least once, but was never performed: x => x.ShouldBeCalled()”

It is just a guess but Is Moq overriding the Save() method with it’s own version which ignores anything I have inside the real object’s Save() method.

  • 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-12T12:22:29+00:00Added an answer on May 12, 2026 at 12:22 pm

    You are having this problem because you are mocking what you are testing. This doesn’t make sense.

    You are correct that Moq will replace the implementation of your method with its own. The reason is you are supposed to use Moq to mock things the class you are testing calls, not the class you are testing itself.

    This test would be appropriate if your code were designed thusly:

    public class ClassA
    {
        BusinessLogicClass bl;
        public ClassA(BusinessLogicClass bl)
        {
             this.bl = bl;
        }
    
        public void Save()
        {
            bl.ShouldBeCalled();
        }
    }
    
    public class BusinessLogicClass
    {
        public virtual void ShouldBeCalled()
        {
            //This should get executed
        }
    }
    

    And here is the correct test of that method now:

    [TestFixture]
    public class ClassA_Test
    {
        [Test]
        public void Save_ShouldCallShouldBeCalled()
        {
            //Arrange
            var mockBLClass = new Mock<BusinessLogicClass>();
            mockBLClass.Setup(x => x.ShouldBeCalled()).Verifyable();
    
            //Act    
            ClassA classA = new ClassA(mockBLClass.Object);
            classA.Save();
    
            //Assert
            mockBLClass.VerifyAll();
        }
    }
    

    The key lesson here is that you mock/stub what your test needs to run, not what you are testing itself.

    Hope this helps,
    Anderson

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

Sidebar

Related Questions

This seems like it should be something fairly simple, but I just can't seem
This seems like I'm missing something obvious but I can't get redirects (>) to
This seems like it should be simple, but I can't seem to find a
This seems to me like a very simple question, but I can't seem to
This seems like it should be easy as pie, but I can't seem to
This seems like it should be fairly simple, but for some reason I can't
This seems fairly simple but I can't get it to turn up on Google.
These seems like something very simple, but search as I might I just can't
Seems like I may have missed something simple in the syntax, but I'd like
This is probably something extremely simple but I can't get my head around it

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.