是否可以在xUnit中使用依赖注入? [英] Is it possible to use Dependency Injection with xUnit?
本文介绍了是否可以在xUnit中使用依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有需要IService的构造函数的测试类。
I have a test class with a constructor that needs an IService.
public class ConsumerTests
{
private readonly IService _service;
public ConsumerTests(IService servie)
{
_service = service;
}
[Fact]
public void Should_()
{
//use _service
}
}
我想插入我选择的DI容器以构建测试类。
使用 xUnit 可以吗?
推荐答案
是的,可以使用Xunit.DependencyInjection
Yes it's possible with Xunit.DependencyInjection
Install-Package Xunit.DependencyInjection
您可以注入服务
[assembly: TestFramework("Your.Test.Project.Startup", "AssemblyName")]
namespace Your.Test.Project
{
public class Startup : DependencyInjectionTestFramework
{
public Startup(IMessageSink messageSink) : base(messageSink) { }
protected override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IDependency, DependencyClass>();
}
}
}
https://github.com/pengweiqhca/Xunit.DependencyInjection
这篇关于是否可以在xUnit中使用依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文