Dependency Injection with XUnit and ASP.NET Core 1.0

…衆ロ難τιáo~ 提交于 2019-12-03 13:02:29
Danny van der Kraan

Well, I don't think it is possible to access the container of the SUT. And to be honest I don't exactly understand why you'd want to. You will want complete control of your SUT. That means you want to provide your own dependencies to inject.

And that, you can!

_server = new TestServer(TestServer.CreateBuilder(null, app =>
{
    app.UsePrimeCheckerMiddleware();
},
services =>
{
    services.AddSingleton<IPrimeService, NegativePrimeService>();
    services.AddSingleton<IPrimeCheckerOptions, PrimeCheckerOptions>();
}));

The CreateBuilder provides overloads for this. You'll need to provide configurations and app configurations for the same reasons (reason being you want complete control over your SUT). I followed this article to make the above example if you are interested. I could also upload the sample to my GitHub if you want?

Let me know if it helped.

Update GitHub sample: https://github.com/DannyvanderKraan/ASPNETCoreAndXUnit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!