Aspnetcore.docs: Inject Mock

Created on 12 Mar 2019  Â·  7Comments  Â·  Source: dotnet/AspNetCore.Docs

How to inject Mock<IQuoteService> instead of concentrate class when testing?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

PU Source - Docs.ms doc-enhancement

All 7 comments

Hello @Lechus ... The approach shown in the topic (ConfigureTestServices to use the test service) in the Inject mock services section is the only approach recommended by the engineers. If you have a general question about alternative approaches for mock service injection, we recommend asking on a support forum, such as Stack Overflow, or a support chat, such as Slack or Gitter.

Can this please be reopened?

The approach in the documentation creates a custom stub. A lot of devs will be trying to use Moq. An example that shows how to use Moq.Mock<T>, setup some mock invocations and then verify them.

Moq is shown in the RP unit test topic.

I'll re-open and put this on the Integration Testing project, but I'm not sure if or when it will be worked. We don't work on this topic unless its a version update or a bug fix. This topic's content is directly managed by engineering.

@javiercn I'm running through the Testing project today. There's a few that you'll need to look at in time, but I'll ping u on this one now because I don't think we'll be showing Moq in the integration testing topic+sample. We have Moq coverage in Test controller logic in ASP.NET Core and Razor Pages unit tests in ASP.NET Core (we cross-link both in the integration testing topic), and I think that's as far as we'll go unless you say otherwise. If so, then we can close this.

I believe I filed a separate issue to explain how DI+Hosting works, so that we can point people there when they aren't sure about overriding/injecting services, which is one of the most common sources of pain and which I think would cover that. You can reference that issue in here and close it?

I've blogged about ASP.NET Core Integration Testing & Mocking using Moq. It's not simple and requires some setup but I hope it helps someone out.

Was this page helpful?
0 / 5 - 0 ratings