Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor arguments cannot be passed for interface mocks

Tags:

c#

moq

When I debug the code and read the line with the mockLessonplannerAFactory creation I get the error:

Constructor arguments cannot be passed for interface mocks.

var mockSchoolclassCodeService = new Mock<ISchoolclassCodeService>();
var mockDateService = new Mock<IDateService>();
var mockLessonplannerAFactory = new Mock<ILessonplannerAFactory>(mockDateService.Object);
var mockLessonplannerBFactory = new Mock<ILessonplannerBFactory>(mockDateService.Object);

var service = new TimeTableService(mockUnitOfWork.Object, mockLessonplannerAFactory.Object, mockLessonplannerBFactory.Object, mockSchoolclassCodeService.Object);

My TimeTableService accepts instances of an interface type only. But the mockLessonplannerAFactory and BFactory... want in their constructor also an IDateService passed.

What is wrong with my code?

like image 394
Elisabeth Avatar asked Mar 16 '13 11:03

Elisabeth


2 Answers

The mock that is created from the interface will have a default constructor because interfaces don't have a constructor. Remember you are mocking the interface not the concrete class.

"But the mockLessonplannerAFactory and BFactory... want in their constructor also an IDateService passed."

They are both mocked from interfaces, so there is no constructor. The mock class will create a default constructor which doesn't need to be passed anything.

like image 121
Colin Mackay Avatar answered Oct 19 '22 17:10

Colin Mackay


What you simply wanted to do...

1)create a normal interface mock.

  var mockDateService = new Mock<IDateService>();

2)Do a set up for accepting thing way that you need as constructor parameter.

mockDateService .Setup(s => s.ConstructorParam).Returns(constructorParamValue);
like image 43
Prageeth godage Avatar answered Oct 19 '22 16:10

Prageeth godage