相关疑难解决方法(0)

NET NET:在Xunit Test中为AppService,存储库等执行所有依赖注入

我正在尝试在XUnit测试中为AppService实现依赖注入。理想的目标是运行原始应用程序启动/配置,并使用启动中的任何依赖项注入,而不是在测试中再次重新初始化所有DI,这就是整个目标。

更新:Mohsen的答案很接近。需要更新几个语法/要求错误才能起作用。

由于某种原因,原始应用程序可以工作,并且可以致电Department App Service。但是,它无法在Xunit中调用。最终使Testserver在原始应用程序中使用Startup和Configuration进行工作。现在收到以下错误:

Message: The following constructor parameters did not have matching fixture data: IDepartmentAppService departmentAppService

namespace Testing.IntegrationTests
{
    public class DepartmentAppServiceTest
    {
        public DBContext context;
        public IDepartmentAppService departmentAppService;

        public DepartmentAppServiceTest(IDepartmentAppService departmentAppService)
        {
            this.departmentAppService = departmentAppService;
        }

        [Fact]
        public async Task Get_DepartmentById_Are_Equal()
        {
            var options = new DbContextOptionsBuilder<SharedServicesContext>()
                .UseInMemoryDatabase(databaseName: "TestDatabase")
                .Options;
            context = new DBContext(options);

            TestServer _server = new TestServer(new WebHostBuilder()
                .UseContentRoot("C:\\OriginalApplication")
                .UseEnvironment("Development")
                .UseConfiguration(new ConfigurationBuilder()
                    .SetBasePath("C:\\OriginalApplication")
                    .AddJsonFile("appsettings.json")
                    .Build()).UseStartup<Startup>());

            context.Department.Add(new Department { DepartmentId = 2, DepartmentCode = "123", DepartmentName …
Run Code Online (Sandbox Code Playgroud)

.net c# xunit .net-core asp.net-core

12
推荐指数
3
解决办法
1758
查看次数

如何使用 XUnit 依赖构造函数注入来创建我的具体类

我获取了 xunit.DependencyInjection 包并使用我的接口创建了我的构造。测试用例可以编译,但是当我运行 xunits 时,它不会执行构造函数依赖注入。

 public class TestSuite{
  IARepository _aRepository;
  IBRepository _bRepository;
    public TestSuite(IARepository aRepository, IBRepository bRepository)
    {
        _aRepository = aRepository;
        _bRepository = bRepository;
    }
}
Run Code Online (Sandbox Code Playgroud)

GitHub 建议构造函数注入是可能的: https://github.com/pengweiqhca/Xunit.DependencyInjection/tree/master/Xunit.DependencyInjection.Test

启动.cs

 public class Startup
 {
    public void ConfigureServices(IServiceCollection services)
    {

    var configuration = new ConfigurationBuilder()
            .SetBasePath(System.IO.Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", false, true)
            .Build();

                    
        var connectionString =     configuration.GetConnectionString("A_DbCoreConnectionString");
        services.AddDbContext<AContext>(options1 => options1.UseSqlServer(connectionString));

        connectionString= configuration.GetConnectionString("B_DbCoreConnectionString");
        services.AddDbContext<BContext>(options2 => options2.UseSqlServer(connectionString));

        services.AddTransient<IARepository, ARepository>();
        services.AddTransient<IBRepository, BRepository>();
    }
  }
Run Code Online (Sandbox Code Playgroud)

A和B存储库.cs

public class ARepository :IARepository
{
    public AContext _dbContext; 
    public ARepository(AContext dbContext) 
    { …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection

8
推荐指数
1
解决办法
5397
查看次数

标签 统计

c# ×2

.net ×1

.net-core ×1

asp.net-core ×1

dependency-injection ×1

xunit ×1