如何在 xUnit Test 项目中使用 DbContext?

6 c# asp.net unit-testing asp.net-core

我为我的核心 1.0 应用程序添加了一个测试项目。其中我有在构造函数中使用 dbContext 的存储库。

如何在测试项目中访问此 dbContext 并正确注入它?

这是我的 ApplicationDbContext 类:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ShoppingCartItem> ShoppingCartItems { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试方法:

[Fact]
public void ProductsGetAll()
        {
        //here i need to get acces to dbContext and inject it below

            ProductRepository productRepository = new ProductRepository(dbContext);
            CategoryRepository categoryRepository = new CategoryRepository(dbContext);
            ProductController productController = new ProductController(productRepository, categoryRepository);
        }
Run Code Online (Sandbox Code Playgroud)

更新

我在测试构造函数中添加了上下文:

ApplicationDbContext dbContext;

    public UnitTest1()
    {
        var options = new DbContextOptionsBuilder<ApplicationDbContext>()
             .UseInMemoryDatabase(databaseName: "FakeDatabase")
             .Options;

        dbContext = new ApplicationDbContext(options);
    }
Run Code Online (Sandbox Code Playgroud)

和我的测试方法:

    [Fact]
        public void ProductsGetAll()
        {
            IProductRepository productRepository = new ProductRepository(dbContext);
            ICategoryRepository categoryRepository = new CategoryRepository(dbContext);
            ProductController productController = new ProductController(productRepository, categoryRepository);

            var result = productController.List("Pizza");

            var contentResult = result as ViewResult;
            var final = contentResult != null;
            Assert.False(final, "Shouldnt be null");
Run Code Online (Sandbox Code Playgroud)

但现在我有一个错误:

Message: System.NullReferenceException : Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

noz*_*man 2

我不是 .NET Core 人员,但我想您可以尝试使用内存数据库进行单元测试。

在旧版本的 EntityFramework 中,您可以使用Effort,EF Core 似乎已经解决了这个问题。我不确定这是否会立即起作用,因为我自己从未尝试过这个(同样,不是 .net core 人员),但你应该像这样开始:

首先,添加包Microsoft.EntityFrameworkCore.InMemory
然后创建上下文并将其配置为使用内存数据库

var options = new DbContextOptionsBuilder<ApplicationDbContext>()
    .UseInMemoryDatabase(databaseName: "pick_or_generate_a_unique_name_here")
    .Options;

var context = new ApplicationDbContext(options);
Run Code Online (Sandbox Code Playgroud)

最后,将上下文注入您的存储库并在测试中使用它们

var productRepository = new ProductRepository(context);
var categoryRepository = new CategoryRepository(context);
var productController = new ProductController(productRepository, categoryRepository);
Run Code Online (Sandbox Code Playgroud)

请参阅此博文MSDN了解更多信息。