XUnit Create Operation Service Mock Failure

The issue is that the setup expects the specific instance created when GetTestClinicModel() is invoked.

However that same instance is not used when exercising the test as a totally new instance is created when GetTestClinicModel() is invoked again.

Thus the mock will return null since there are separate instances.

Consider changing the setup to use an argument matcher like It.Is<T>()

serviceMock
    .Setup(_ => _.AddClinic(It.Is<CreateClinicBindingModel>(m => m.Name == "Clinic-3")))
    .Returns(GetTestClinic());

The above setup tells the mock to behave as expected when it gets an instance that matches the provided predicate

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top