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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:16:25+00:00 2026-06-05T08:16:25+00:00

Update 1, more details My question was hard to interpret, sorry for that…. Simple

  • 0

Update 1, more details

My question was hard to interpret, sorry for that….

Simple question…
How do you test a specific implementation with Moq?

interface IShipping
{
    int CalculateCost();
}

class TruckShipping: IShipping
{
    int CalculateCost()
    {
        return 9;
    }
}

class Shipping: IShipping
{
    int CalculateCost()
    {
        return 5;
    }
}

class CalculateSomethingMore
{
    IShipping _shipping;
    CalculateSomethingMore(IShipping shipping)
    {
        // Here I want the TruckShipping implementation!
        _shipping = shipping;
    }
    DoCalc()
    {
        return _shipping.CalculateCost * 2;
    }
 }

Without mock it would probably look like (if you don’t use DI)
TEST:

var truckShipping = new TruckShipping();
var advancedCalculation = CalculateSomethingMore(truckShipping);
var result = DoCalc();

var expected = 18;
Assert.IsTrue(result == expected);

NUnit, FluentAssertions, MbUnit, xUnit, etc.. doesn’t matter 🙂

Test:
var truckShipping = Mock.Of<IShipping> …. ? I want to test the TruckShipping implementation.

and inject that into CalculateSomethingMore.

  • 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-05T08:16:27+00:00Added an answer on June 5, 2026 at 8:16 am

    If you want to test TruckShipping implementation then you need separate tests for TruckShipping implementation.

    Currently you are testing behavior of CalculateSomethingMore, which should return doubled value of cost, calculated by shipping. You don’t care which shipping. Responsibility of CalculateSomethingMore is asking shipping about it’s cost, and doubling that cost:

    Mock<IShipping> shipping = new Mock<IShipping>();
    shipping.Setup(s => s.CalculateCost()).Returns(10);
    CalculateSomethingMore sut = new CalculateSomethingMore(shipping.Object);
    Assert.That(20, Is.EqualTo(sut.DoCalc()));
    shipping.VerifyAll();
    

    You can see, that this test uses neither 9 nor 5 as shipping cost. Why? Because you actually don’t care what value it will have. Calculating shipping cost is not responsibility of class under test.

    Also you need another test for your TruckShipping implementation, which will verify, that TruckShipping calculates shipping cost correctly (this is a responsibility of your shipping object):

    TruckShipping sut = new TruckShipping();
    Assert.That(9, Is.EqualTo(sut.DoCalc());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Update] For more details, The reason that why I try to do pure Git
Update: After some more reading I see that this problem is totally general, you
Update: I've fixed up my referencing below, and my problem is now more specific.
Simple (I hope), HTML question. Let's say I have a column group that spans
I'm attempt to establish a grid and update it with more records via JSON.
I found some problem. When i running apc_store and more times update a page
I am trying to update a nested collection using the Patch API. More specifically,
UPDATED: Added one more question (Question #4). Hi all, I'm building myself a custom
Update 2018 : This question was asked long before PostCSS existed, and I would
This is a big-picture question... but I also have details on the problem 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.