Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 742
John Nicholas Avatar asked Aug 17 '10 15:08

John Nicholas


People also ask

Can you mock an interface?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.

What can be mocked with Moq?

Unit testing is a powerful way to ensure that your code works as intended. It's a great way to combat the common “works on my machine” problem. Using Moq, you can mock out dependencies and make sure that you are testing the code in isolation.

Can you mock without an interface?

Sometimes you come across a class that you need to stub or mock but it does not have an interface. The easiest way is of course to add an interface (duh) but that might not always be possible. Here are three methods you can use to stub or mock a class without an interface.

Can you mock a class with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.


1 Answers

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.

like image 130
Håvard S Avatar answered Oct 09 '22 23:10

Håvard S