I have a concrete class that I am mocking using Mock.Of
, but it does not have a default constructor. Is there any way to get this to work without having to interface this or using new Mock
? The former is overkill at this time, and the latter is uglier. I am going to try creating an extension method that returns a new Mock().Object
, however I do not think that Mock.Get
will work in that scenario
Asked
Active
Viewed 5,890 times
7

Justin Pihony
- 66,056
- 18
- 147
- 180
-
Related: http://stackoverflow.com/questions/7414704/mocking-objects-with-moq-when-constructor-has-parameters – Robert Harvey May 01 '13 at 17:31
-
I looked at that, but it does not solve my problem – Justin Pihony May 01 '13 at 17:36
1 Answers
6
It turns out that Moq does track objects created normally (with new
and pass back Object
) so that they can be used in Mock.Get
. So, this works for me. I would still take a built in way if there is one:
public static T MockOf<T>(params Object[] args) where T : class
{
return new Mock<T>(args).Object;
}

Justin Pihony
- 66,056
- 18
- 147
- 180