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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:52:26+00:00 2026-05-16T11:52:26+00:00

what I want to do is construct a moq for I1 – which is

  • 0

what I want to do is construct a moq for I1 – which is fine … however in the course of the method that I am testing that uses this mock I need to cast it to I2 in order to access some properties that are not on I1

Interface I1 
{ int AProperty{get;set;}}

Interface I2
{int AnotherProperty{get;set;}}

I then have some objects

Class O1 : I1 {}

and

Class O2 : O1 , I2 {}

the problem is that when i have an instance of a I2 implementing object I can cast it to I1 in order to access the methods that are implmented through that interface. In code this is not a problem and everythign works as expected.

The only problem comes when writing a unit test on that class.

The interfaces also expose a method called GetNewInstance which returns an initialised instance of the the implementing object cast into the IGetNewInstance interface … i can usually mock this fine and make it return itself (and so I keep working with the mock object).

however when you try to cast this returned object of type I2 into I1 a null reference results – this makes sense as the mock object that implements I2 does not inherit from anything that inherits I1.

the question is how can i force the mock object to inherit from both I1 ansd I2 at the same time?

  • 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-16T11:52:27+00:00Added an answer on May 16, 2026 at 11:52 am

    The way I understand you, you want to create a mock that implements two interfaces. With Moq, that is as simple as this:

    var mock = new Mock<IFoo>(); // Creates a mock from IFoo
    mock.As<IBar>(); // Adds IBar to the mock
    mock.As<IBar>().Setup(m => m.BarMethod()).Returns(new object()); // For setups.
    

    Now, you can set up expectations and use your mock as you would normally use the object implementing both IFoo and IBar.

    For your GetNewInstance method, you can just set up an expectation that returns the mock itself.

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

Sidebar

Related Questions

I want to dynamically construct a TreeView using Node which represents a typical tree
I'm predominately a Java guy, which is why I need some assistance on what
I am using maven-assembly-plugin to construct an assembly. I want to include in the
I'm trying to run the following code with Ninject.Moq: [TestMethod] public void TestMethod1() {
I am trying to unit test an action filter I wrote. I want to
I want to create a PDO class for handling data base connections. Here is
I'm not sure why the following doesn't work. StructureMap tells me there is no
Does Qt has something like QSettings , but for local scopes? I am seeking
can anyone generate a query for me. Lets say i have a table sales(saleID,

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.