MOQ - how to mock an interface that needs to be cast to another interface?

32,519

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.

Share:
32,519

Related videos on Youtube

John Nicholas
Author by

John Nicholas

slightly crazed .net junkie. Developer since 2000. Consciously decided to go the MS route due to their tooling. fairly serious guitarist (7 years or so - currently learning lots of old blues, Howling wolf etc) and am increasingly spending time getting back into Go. Current specialisms: SQL, EF40 + T4, WCF, linq, lambda, silverlight4, moq, pex, moles - anything needed to get the job done really.

Updated on December 01, 2020

Comments

  • John Nicholas
    John Nicholas over 3 years

    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?

  • John Nicholas
    John Nicholas almost 14 years
    BTW to anyone reading when you do the setups you have to use the mock.AS<IBAR>.Setup(......)
  • gakera
    gakera about 7 years
    Awesome, this works great. If you need to do the .Setup you can do it all in one line, you don't need to do the As<> on it's own first, you can just do it all in one line.
  • Subjective Reality
    Subjective Reality over 3 years
    Is there a way to do this setup as a non-interface?
  • John Nicholas
    John Nicholas over 3 years
    I actually do not use Moq anymore. I far it far far simpler just to manually create mocks - it also forces me to really think about what i am doing. When using something like resharper / rider with decent refactor tools it is so much simpler - it helps people realise that they are often not testing anything of substance.