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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:40:12+00:00 2026-06-05T18:40:12+00:00

I am new to unit testing and TDD and mocking in general, however the

  • 0

I am new to unit testing and TDD and mocking in general, however the general idea I understand. My question is how do I mock a class so that I can call the instantiated method without having duplicate code in my unit test and in my Implementing class? Given the following:

//Simple Interface
public interface IProduct {
    double price { get; set; }
    double tax { get; set; }
    double calculateCost();
}

//Simple Implementation of IProduct
public class SimpleProduct : IProduct {
    private double _price;
    private double _tax;

    public double price {
        get { return _price; }
        set { _price = value; }
    }

    public double tax {
        get { return _tax; }
        set { _tax = value; }
    }

    public double calculateCost() {
        return _price + (_price * _tax);
    }
}
//Complex implementation of IProduct
public class MarylandProduct : IProduct {
    private double _price;
    private double _tax;

    public double price {
        get { return _price; }
        set { _price = value; }
    }

    public double tax {
        get { return _tax; }
        set { _tax = value; }
    }

    public double calculateCost() {
        if (_price <= 100) return _price + (_price * _tax);
        else {
            double returnValue = 100 + (100 * _tax); //use tax rate for first 100
            returnValue += (_price - 100) + ((_price - 100) * 0.05); //use a flat rate of 0.05 for everything over 100
            return returnValue;
        }
    }
}

I have started writing a unit test that has the following:

[TestMethod]
[HostType("Moles")]
public void molesCalculateCostforMarylandProduct() {
    //Assign
    MMarylandProduct marylandProduct = new MMarylandProduct();
    marylandProduct.priceGet = () => 1000;
    marylandProduct.taxGet = () => 0.07;

    const double EXPECTED = 1052;

    //Act
    double actual = marylandProduct.Instance.calculateCost();

    //Assert
    Assert.AreEqual(EXPECTED, actual);
}

I want to be able to call the calculate cost method for either a MarylandProduct or a SimpleProduct in my unit test. Typically getting the price and tax comes from the database, but instead I have made it so that these values are stubbed to avoid any coupling with a database or service or whatever else that provides these values. What it comes down to is that I want to write a unit test that will test the functionality of calculateCost() without having to stub that method in the unit test because I know that in 2 years the logic in the MarylandProduct will change.

So for example once I have this test running I should be able to go in and change the code for MarylandProduct.calculateCost() to add a “luxury tax” of adding 50 to any price over say 750. If I do this, I know that my unit test will fail because the expected value is 1052 and now the MarylandProduct is returning something other than what is expected.

Am I just going about this the wrong way? Am I just missing the spirit of TDD?
Thanks for any help.

EDIT: (adding in other mocking frameworks that I have tried)

[TestMethod]
    public void rhinoMockCalculateCostForMarylandProduct() {
        //assign
        IProduct marylandProduct = MockRepository.GenerateMock<IProduct>();
        marylandProduct.Stub(price => price.price).Return(1000);
        marylandProduct.Stub(tax => tax.tax).Return(0.07);

        const double EXPECTED = 1052;

        //act
        double actual = marylandProduct.calculateCost();

        //assert
        Assert.AreEqual(EXPECTED, actual);
    }

    [TestMethod]
    public void moqCalculateCostForMarylandProduct() {
        //assign
        var marylandProduct = new Mock<IProduct>();
        marylandProduct.Setup(price => price.price).Returns(1000);
        marylandProduct.Setup(tax => tax.tax).Returns(0.07);

        const double EXPECTED = 1052;

        //act
        double actual = ((MarylandProduct)marylandProduct.Object).calculateCost();

        //assert
        Assert.AreEqual(EXPECTED, actual);
    }

I want to avoid putting duplicate code in the unit test and in the implementation of the class because if the code in the class changes then the unit test will still pass because it hasn’t been changed. I know that it is expected to make that change in the unit test however when you have a TON of unit tests is it acceptable to have this sort of design? Having duplicate code in your unit test and in your implementation?

  • 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-05T18:40:15+00:00Added an answer on June 5, 2026 at 6:40 pm

    Ok so, what you seem to be misunderstanding is the purpose of a Mock. A mock is used to isolate the system under test (SUT). Therefore you mock the SUT’s dependencies, so that you can test the SUT in isolation (e.g. remove dependency on the db or some service). You do not mock the object you are testing, otherwise, what are you testing?

    If I understand your question correctly your domain model has an implementation of IProduct (why you need different impls of a Product is another question).

    You want to test that implementations’s CalculateCost method. I see no reason why you need to use mocks for this. From the surface, Product shouldn’t have any dependencies, therefore there is nothing to mock.

    e.g.

    Let’s say you have this product in your domain:

    public class MyProduct : IProduct {
        private double _price;
        private double _tax;
    
        public double Price {
            get { return _price; }
            set { _price = value; }
        }
    
        public double Tax {
            get { return _tax; }
            set { _tax = value; }
        }
    
        public double CalculateCost() {
            return //some complex logic here.
        }
    }
    

    This object is already isolated, it has no dependency, thus there is no need to mock.

    To test it you would simply use it directly:

    [TestMethod]
    public void CalculateCost() {
        //arrange
        var myProduct = new MyProduct();
        myProduct.Price = 1000;
        myProduct.Tax= 0.07;
    
        //act
        double actualCost = myProduct.CalculateCost();
    
        //assert
        double expectedCost = 1052;
    
        Assert.AreEqual(expectedCost, actualCost );
    }
    

    Recommended reading: http://www.manning.com/osherove/

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

Sidebar

Related Questions

New to unit testing and have an app that is spread out over several
I am new to unit testing, I understand the basic concepts, and I am
I am 'relatively new' to unit-testing and TDD. Only more recently have I completed
I read a lot of question and answer on TDD and unit testing on
So, I'm reasonably new to both unit testing and mocking in C# and .NET;
I am new to unit testing/TDD and having problem getting my head certain aspects
I have started practicing TDD approach. I am pretty much new to unit testing.
I'm pretty new to TDD and unit testing and I'm giving it a try
So I know that unit testing is a must. I get the idea that
Are there any good unit testing and acceptance testing frameworks that can be applied

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.