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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:10:55+00:00 2026-05-31T15:10:55+00:00

I would like to run some unit tests on the method below I am

  • 0

I would like to run some unit tests on the method below I am passing a mocked interface(vehicleObject) into ProcessVehicles but as soon as it is passed it gets reassigned by DetermineVehicleType so my mocked object is of no use. My first idea would be to create a boolean to determine if DetermineVehicleType should be run and add it as a param, but that sounds so very messy. Is there a better way to get around this ?

Method with Mock object being injected:

public void ProcessVehicles(ICarObject CarObject)
{
    IObject vehicleObject = DetermineVehicleType(carObject);
    vehicleObject.ProcessVehicle(carObject);
}

Original Code:

public void ProcessVehicles()
{
    IObject vehicleObject = DetermineVehicleType(carObject);
    vehicleObject.ProcessVehicle(carObject);
}

Note: I can’t check if vehicleObject is null before calling DetermineVehicleType because it might not be null when the class is actually used. In the long run maybe total refactor is the answer, at this point that is not the answer I am looking for maybe there is not another option.

the method DetermineVehicleType is private

Note: I know there are code smells this is legacy code that currently works. I want to get tests around it not change it so it looks pretty and then breaks in production. Total refactor might be the only option I just want to make sure there is not another solution with the mock tools.

  • 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-31T15:10:56+00:00Added an answer on May 31, 2026 at 3:10 pm

    What access modifier does DetermineVehicleType have? You could stub out that method so that it returns your mocked interface (Roy Osherove calls this the abstract test driver pattern, I believe). Otherwise, this looks like a prime candidate for refactoring 🙂

    To refactor your code you would do something like this

    First, change your method signature

    protected virtual IObject DetermineVehicleType(CarObject obj)
    {
        //Do whatever you normally do
    }
    

    Then, in your test, you can create a stub out of the above class, and have it return your stubbed IObject no matter the CarObject passed in. You can either manually create a stub class by inheriting from the class, or you could use something like MOQ to accomplish this. Let me know if you need me to elaborate on this a little more.

    Another note, however:

    A better way to refactor this would be to simply pass in the IObject to the ProcessVehicles, as it seems from this example that you have a SRP violation here, where the ProcessVehicles method is doing more than processing them. But, maybe that is just from this simplified example

    FULL Implementation Update

        [Test]
        public void TestMethod()
        {
            var testerStub = new TesterStub();
            testerStub.ProcessVehicles();
            //Assert something here
        }
    
        public class TesterStub : Tester
        {
            public override IObject DetermineVehicleType(CarObject obj)
            {
                var mockObject = new Mock<IObject>();
                mockObject.Setup(x => x.SomeMethod).Returns(Something);
                return mockObject.Object;
            }
        }
    
        public class Tester
        {
            protected virtual IObject DetermineVehicleType(CarObject obj)
            {
                return new ObjectTester();
            }
    
            public void ProcessVehicles()
            {
                var carType = DetermineVehicleType(new CarObject());
    
            }
        }
    
        public class ObjectTester : IObject
        {
        }
    
        public interface IObject
        {
        }
    
        public class CarObject
        {
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have some unit tests that I would like to be executed during
I would like to run some code onload of a form in WPF. Is
In a classes are some methods that are run synchronously. I would like them
I would like to run APIDemos under platforms\android-1.5\samples directory on Android emulator but I
I don't do tests, but I'd like to start. I have some questions: Is
I ran into an odd bug today when I was running some unit tests
We have a lot of unit tests but they aren't run every night. I
In order to reduce code duplication I would like to generate unit tests programatically
I tried recently to use NAnt (beta 0.86.2962.0) to run some unit tests compiled
I want to run some Python unit tests from my Maven module. I have

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.