Is it possible to use Dependency Injection with xUnit?

前端 未结 4 1215
忘掉有多难
忘掉有多难 2021-02-06 22:25

I have a test class with a constructor that needs an IService.

public class ConsumerTests
{
    private readonly IService          


        
4条回答
  •  北海茫月
    2021-02-06 23:03

    Yes it's possible with Xunit.DependencyInjection

    Install-Package Xunit.DependencyInjection
    

    and you can inject your services

    [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();
            }
        }
    }
    

    https://github.com/pengweiqhca/Xunit.DependencyInjection

提交回复
热议问题