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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 238k
  • Answers 238k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer int** is not 2D array, it is rather an array… May 13, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer Normalize both strings and compare them: str1.toLowerCase().replace(/[^a-z0-9,;]+/g, "") == str2.toLowerCase().replace(/[^a-z0-9,;]+/g,… May 13, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer Compound indexes should be strings, so use the appropriate function… May 13, 2026 at 6:52 am

Related Questions

This seems like something simple but I can't seem to get it to work.
Seems like I may have missed something simple in the syntax, but I'd like
I need to run two programs in sequence as part of a custom builder.
This seems like a really simple question but I haven't seen it answered anywhere.
I've enjoyed building out a couple simple applications on the GAE, but now I'm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.