Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock.Of<T> with constructor arguments

Tags:

c#

.net-4.0

moq

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

like image 545
Justin Pihony Avatar asked May 01 '13 17:05

Justin Pihony


1 Answers

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;
}
like image 113
Justin Pihony Avatar answered Nov 12 '22 11:11

Justin Pihony