相关疑难解决方法(0)

使用ASP.NET Core DI解析实例

如何使用ASP.NET Core MVC内置依赖注入框架手动解析类型?

设置容器很容易:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddTransient<ISomeService, SomeConcreteService>();
}
Run Code Online (Sandbox Code Playgroud)

但是如何在ISomeService不进行注射的情况下解决?例如,我想这样做:

ISomeService service = services.Resolve<ISomeService>();
Run Code Online (Sandbox Code Playgroud)

没有这样的方法IServiceCollection.

c# dependency-injection asp.net-core-mvc asp.net-core

245
推荐指数
6
解决办法
17万
查看次数

如何使用构造函数依赖注入单元测试asp.net核心应用程序

我有一个asp.net核心应用程序,它使用在应用程序的startup.cs类中定义的依赖注入:

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:FotballConnection:DefaultConnection"]));


        // Repositories
        services.AddScoped<IUserRepository, UserRepository>();
        services.AddScoped<IUserRoleRepository, UserRoleRepository>();
        services.AddScoped<IRoleRepository, RoleRepository>();
        services.AddScoped<ILoggingRepository, LoggingRepository>();

        // Services
        services.AddScoped<IMembershipService, MembershipService>();
        services.AddScoped<IEncryptionService, EncryptionService>();

        // new repos
        services.AddScoped<IMatchService, MatchService>();
        services.AddScoped<IMatchRepository, MatchRepository>();
        services.AddScoped<IMatchBetRepository, MatchBetRepository>();
        services.AddScoped<ITeamRepository, TeamRepository>();

        services.AddScoped<IFootballAPI, FootballAPIService>();
Run Code Online (Sandbox Code Playgroud)

这允许这样的事情:

[Route("api/[controller]")]
public class MatchController : AuthorizedController
{
    private readonly IMatchService _matchService;
    private readonly IMatchRepository _matchRepository;
    private readonly IMatchBetRepository _matchBetRepository;
    private readonly IUserRepository _userRepository;
    private readonly ILoggingRepository _loggingRepository;

    public MatchController(IMatchService matchService, IMatchRepository matchRepository, IMatchBetRepository matchBetRepository, ILoggingRepository loggingRepository, IUserRepository userRepository)
    {
        _matchService = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net testing unit-testing dependency-injection

49
推荐指数
5
解决办法
4万
查看次数

是否可以在xUnit中使用依赖注入?

我有一个测试类,其中包含一个需要IService的构造函数.

public class ConsumerTests
{
    private readonly IService _service;
    public ConsumerTests(IService servie)
    {
      _service = service;
    }

    [Fact]
    public void Should_()
    {
       //use _service
    }
}
Run Code Online (Sandbox Code Playgroud)

我想插入我选择的DI容器来构建测试类.

这可能与xUnit一起使用吗?

testing unit-testing dependency-injection xunit ioc-container

21
推荐指数
4
解决办法
1万
查看次数

Xunit项目中的依赖注入

我正在研究ASP.Net Core MVC Web应用程序。

我的解决方案包含2个项目:一个用于应用程序,另一个用于单元测试。

我在“测试”项目中添加了对应用程序项目的引用。

我现在要做的是在Tests项目中编写一个类,该类将通过实体框架与数据库进行通信。

我在应用程序项目中所做的工作是通过构造函数依赖项注入来访问DbContext类。

但是我无法在测试项目中执行此操作,因为我没有Startup.cs文件。在此文件中,我可以声明哪些服务可用。

那么,如何在测试类中获取对DbContext实例的引用?

谢谢

dependency-injection xunit asp.net-core-mvc

9
推荐指数
2
解决办法
8245
查看次数

使用XUnit和ASP.NET Core 1.0进行依赖注入

我试图找出如何使用XUnit的依赖注入.我的目标是能够将我的ProductRepository注入我的测试类.

这是我正在尝试的代码:

public class DatabaseFixture : IDisposable
{
    private readonly TestServer _server;

    public DatabaseFixture()
    {
        _server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
    }

    public void Dispose()
    {
        // ... clean up test data from the database ...
    }
}

public class MyTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture _fixture;
    public ICustomerRepository _repository { get; set; }

    public MyTests(DatabaseFixture fixture, ICustomerRepository repository)
    {
        _fixture = fixture;
        _repository = repository;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误: 以下构造函数参数没有匹配的fixture数据(ICustomerRepository存储库)

这让我相信XUnit不支持依赖注入,只有它是一个Fixture.

有人能给我一种使用XUnit在我的测试类中获取ProductRepository实例的方法吗?我相信我正确启动了测试服务器,因此Startup.cs运行并配置DI.

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

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

尝试通过 XUnit 使用依赖项注入时出现未解析的构造函数参数错误

我的每个 XUnit 测试项目都有一个 appsettings.json 文件,该文件指定了一些设置。我一直使用 IConfiguration 的依赖注入来检索我的 Test Fixture 类中的这些设置。现在想起来,我完全不知道 IConfiguration 过去是如何解决的,因为 XUnit 项目中没有 Startup.cs 和 ConfigureServices 方法。但我发誓它有效。

以下内容曾经有效,但现在不起作用:

夹具:

public class TestFixture : IDisposable
{
    public IConfiguration Configuration { get; set; }

    public TestFixture(IConfiguration configuration)
    {
        Configuration = configuration;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试类:

public class TestCases : IClassFixture<TestFixture>
{
    public TestCases(TestFixture fixture)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误如下:

消息:System.AggregateException:发生一个或多个错误。---- 类装置类型“MyProj.Tests.TestFixture”具有一个或多个未解析的构造函数参数:IConfiguration 配置 ---- 以下构造函数参数没有匹配的装置数据:TestFixture 装置

c# xunit xunit.net asp.net-core

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

创建 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
查看次数

XUnit DI通过覆盖的启动文件(.net核心)

我已经构建了一个WebAPI,除了在Postman上运行的测试之外,我还想实现一些集成/单元测试。

现在,我的业务逻辑非常薄,大多数时候大部分都是CRUD操作,因此我想从测试控制器开始。

我有一个基本设置。存储库模式(接口),服务(业务逻辑)和控制器。流程进入控制器(DI服务)->服务(DI回购)->回购操作!

所以我所做的就是重写我的启动文件,将其更改为内存数据库,其余的应该没问题(我想)是添加了服务,添加了回购协议,现在我指向的是内存数据库,这对于我的基本数据库是很好的测试。

namespace API.UnitTests
{    
    public class TestStartup : Startup
    {
        public TestStartup(IHostingEnvironment env)
            : base(env)
        {

        }

        public void ConfigureTestServices(IServiceCollection services)
        {
            base.ConfigureServices(services);
            //services.Replace<IService, IMockedService>();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            base.Configure(app, env, loggerFactory);
        }

        public override void SetUpDataBase(IServiceCollection services)
        {
            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

            services
                .AddEntityFrameworkSqlite()
                .AddDbContext<ApplicationDbContext>(
                    options => options.UseSqlite(connection)
                );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我编写了第一个测试,但是不存在DatasourceService:

以下构造函数参数没有匹配的灯具数据:DatasourceService datasourceService …

testing xunit asp.net-core

4
推荐指数
2
解决办法
4568
查看次数

如何为使用AutoMapper和依赖注入的.net core 2.0服务编写xUnit测试?

我是.net core/C#编程的新手(来自Java)

我有以下Service类,它使用依赖注入来获取AutoMapper对象和数据存储库对象,以用于创建SubmissionCategoryViewModel对象的集合:

public class SubmissionCategoryService : ISubmissionCategoryService
{

    private readonly IMapper _mapper;

    private readonly ISubmissionCategoryRepository _submissionCategoryRepository;

    public SubmissionCategoryService(IMapper mapper, ISubmissionCategoryRepository submissionCategoryRepository)
    {

        _mapper = mapper;

        _submissionCategoryRepository = submissionCategoryRepository;

    }

    public List<SubmissionCategoryViewModel> GetSubmissionCategories(int ConferenceId)
    {


        List<SubmissionCategoryViewModel> submissionCategoriesViewModelList = 
            _mapper.Map<IEnumerable<SubmissionCategory>, List<SubmissionCategoryViewModel>>(_submissionCategoryRepository.GetSubmissionCategories(ConferenceId) );

        return submissionCategoriesViewModelList;


    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Xunit编写单元测试.我无法弄清楚如何编写方法GetSubmissionCategories一个单元测试,并有我的测试类提供一个IMapper实施和执行ISubmissionCategoryRepository.

到目前为止,我的研究表明我可以创建依赖对象的测试实现(例如SubmissionCategoryRepositoryForTesting),或者我可以使用模拟库来创建依赖关系接口的模拟.

但我不知道如何创建AutoMapper的测试实例或AutoMapper的模拟.

如果你知道任何好的在线教程,详细介绍如何创建一个单元测试,其中被测试的类使用AutoMapper和依赖注入数据存储库,这将是伟大的.

感谢您的帮助.

c# unit-testing xunit automapper asp.net-core-2.0

3
推荐指数
1
解决办法
2639
查看次数