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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:10:04+00:00 2026-05-12T11:10:04+00:00

I have a class that has a property that I need to stub. I

  • 0

I have a class that has a property that I need to stub. I can’t pass it as part of the constructor because the object constructing it does not know the parameters of the constructor.

When running unit tests, I want to be able to have the property be created as a stub.

This is what I have tried, but it does not work:

private DeviceMediator deviceMediator;
private IDeviceControlForm deviceControlForm;
private IDataAccess data;
private ICallMonitor callMonitor;

// Use TestInitialize to run code before running each test 
[TestInitialize()]
public void MyTestInitialize()
{
    // This line works fine
    deviceControlForm = MockRepository.GenerateStub<IDeviceControlForm>();          
    // This line works fine
    data = MockRepository.GenerateStub<IDataAccess>();
    // This has to be an ICallMonitor.  If I try to make it a 
    // CallMonitor then it fails.
    callMonitor = (CallMonitor)
      MockRepository.GenerateStub<ICallMonitor>();
    // This line does not compile.  Because it wants to 
    // return a CallMonitor not an ICallMonitor.
    Expect.Call(new CallMonitor(null)).Return(callMonitor);

    // This is the class that has the CallMonitor (called callMonitor).
    deviceMediator = new DeviceMediator(deviceControlForm, data);
}

Is there anyway to catch the constructor call to CallMonitor and make it actually be a stub?

In case it is relevant, here is the related code in DeviceMediator:

 private IDeviceControlForm form;
 private readonly IDataAccess data;
 public ICallMonitor CallMonitor { get; set; }

 public DeviceMediator(IDeviceControlForm form, IDataAccess data)
 {
     this.form = form;
     this.data = data;
     CallMonitor = new CallMonitor(OnIncomingCall);
 }

Thanks in advance for any help.

  • 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-12T11:10:04+00:00Added an answer on May 12, 2026 at 11:10 am

    Since the CallMonitor property is writable, you can just overwrite the original value with a mock instance (your DeviceMediator actually implements the Property Injection design pattern).

    So you can write a test like this:

    [TestMethod]
    public void MyTest()
    {
        var deviceControlForm = MockRepository.GenerateStub<IDeviceControlForm>();
        var data = MockRepository.GenerateStub<IDataAccess>();
        var mockCallMonitor = MockRepository.GenerateStub<ICallMonitor>();
    
        var deviceMediator = new DeviceMediator(deviceControlForm, data);
        deviceMediator.CallMonitor = mockCallMonitor;
    
        // The rest of the test...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a serializable Message class that has a Data As Object property that
I have a list-generic that has a property (class type). I need a sort
I have class that has a property that is of type string[]. I need
I have a class that has the following property that is generated by the
I have a Menu class that has a IQueryable property called WebPages. In the
I have a class called Question that has a property called Type. Based on
I have come across a class that has an immutable property: MyObject[] allObjs The
I have a class, called DateField , that has a string Value property. If
Lets say I have a LunchBox class with a property for FreezerPack that has
I have a class that has several member classes as attributes. The constructor of

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.