相关疑难解决方法(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
查看次数

创建 TestServer 并使用 XUnit 和 ASP.NET Core 1.0 的依赖注入

我有一个 ASP.NET Core 1.0 解决方案,其项目结构如下:

Web 应用程序 (ASP.NET MVC6)
BusinessLibrary(类库包)
DataLibrary(类库包)测试(带XUnit
的类库包)

我正在尝试在整个系统中使用微软新的内置依赖注入。

以下是当前所有内容如何从我的 ASP.NET MVC 应用程序一直流向我的存储库层

//Startup.cs of MVC Web App
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    services.AddSingleton(_=> Configuration);

    services.AddTransient<ICustomerService, CustomerService>();
    services.AddTransient<ICustomerRepository, CustomerRepository>();
}

public class CustomersController : Controller
{
    private ICustomerService _service;
    public CustomersController(ICustomerService service)
    {
        _service= service;
    }
}

public class CustomerService : ICustomerService
{
    private ICustomerRepository _repository;
    public PriceProtectionManager(ICustomerRepository repository)
    {
        _repository = repository;
    }
}

public class CustomerRepository : BaseRepository, …
Run Code Online (Sandbox Code Playgroud)

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

5
推荐指数
1
解决办法
5043
查看次数

标签 统计

asp.net-core ×2

c# ×2

xunit ×2

.net ×1

.net-core ×1

asp.net ×1

asp.net-core-1.0 ×1