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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:13:30+00:00 2026-06-03T12:13:30+00:00

I am trying to learn how to use Moq to do unit testing with

  • 0

I am trying to learn how to use Moq to do unit testing with C#. I setup a simple class, BasicMath that has a function int Multiply(int a, int b), the body just returns a * b. I have another class called AdvancedMath which has a function ‘int Square(int a)’, the body calls Multiply(a,a) and returns the result.

I am trying to write a simple test for AdvancedMath.Square that will assert that BasicMath.Multiply is called with the correct values. I understand that you cannot do this with ordinary public function so I made the functions virtual, but I am still having trouble. Can someone explain to me how this is done? Neither class has any constructor other than the default empty constructor.

Here is my attempt:

class test_Advanced_Functions
{
    [Test]
    public void test_SquareFunction_Should_return_correct_result()
    {
        // Arrange
        Mock<BasicFunctions> mock = new Mock<BasicFunctions>();
        AdvancedFunctions af = new AdvancedFunctions();

        // Act
        int result = af.Square(5);

        //Assert
        mock.Verify(d => d.Multiply(5, 5), Times.Exactly(1));
    }
  • 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-03T12:13:31+00:00Added an answer on June 3, 2026 at 12:13 pm

    Assuming AdvancedMath derives from BasicMath then you can test that with Moq when Multiply is virtual:

    var mock = new Mock<AdvancedMath>();
    mock.Setup(m => m.Multiply(2, 2)).Returns(4);
    mock.Object.Square(2);
    mock.VerifyAll();
    

    If you use BasicMath within AdvancedMath (ie. if Multiply is static and is invoked directly from Square), then testing such interaction is not possible with Moq. To see why, see this question.

    However, doing behavior based verification in such simple case doesn’t seem right. You need to answer yourself a question: “what do I want to test?”. That Square works or that it calls some methods? This test (what you plan to do now), only delegates responsibility to Multiply, assuming it works. Your Square implementation might change in multiple ways (as in, not using Multiply) and it will break your test – it shouldn’t.

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

Sidebar

Related Questions

I'm trying to learn how to do Unit testing with C# and Moq, and
I'm trying to learn how to use WinForms databinding, but one thing that's confusing
I have been trying to learn to use github, it has been fine until
I am trying to learn how to use the AsyncTask class to authenticate the
I am trying to learn to use the chrome.tabs.executeScript commend. I've created a simple
I am trying to learn how to use JSON and writing a simple program
I'm trying to learn and use SP(Stored Procedure) and UDF(User Defined Function) with MySQL
I'm trying to learn to use Yelp's Python API for MapReduce, MRJob. Their simple
I am trying to learn and use an SDK for a vendor's product. Unfortunately,
I am trying to learn to use buffer overflow attack in Ubuntu. Unfortunately, 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.