aspnet / Testing

[Archived] Testing infrastructure, including support for xUnit.net and StyleCop. Project moved to https://github.com/aspnet/Extensions
66 stars 40 forks source link

Inject IServiceProvider to test case #8

Closed troydai closed 9 years ago

troydai commented 10 years ago

Right now there is no obvious way for test class to consume a IServiceProvider instance. That breaks the scenario which a test case needs to start a self-host web service. The KRunner does have change to obtain a IServiceProvider instance, we needs a mean to inject it to the test case.

Eilon commented 10 years ago

@troydai Can you show a code sample of what the test would look like and what doesn't work? A unit test would generally not use DI at all - it would just create the fake/mock services it needs and pass them to the appropriate constructors. For functional tests this is largely the same story. And for other types of tests the particular DI system being used is set up with all the services that it needs, so the rest of the system just uses the test services as if they are real services (because, well, they are real services).

bricelam commented 10 years ago

Add a reference to Microsoft.Framework.Runtime.Interfaces and use:

var serviceProvider = CallContextServiceLocator.Locator.ServiceProvider;
bricelam commented 10 years ago

@troydai It would be great to wrap this inside of a test fixture or something.

troydai commented 9 years ago

Not needed.