最小起订量和设置数据库上下文 [英] Moq and setting up DB Context

查看:19
本文介绍了最小起订量和设置数据库上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体框架数据库上下文文件.我正在尝试在 NUnit 中设置一个 Moq 框架.目前在 Moq Nunit 测试中收到以下错误.我将如何设置 DBContext,并将项目添加到产品表中?

I have an Entity Framework DB Context file. I am trying to setup a Moq framework in NUnit. Currently receiving error below for Moq Nunit test. How would I setup the DBContext, and add items to a Product Table?

"没有为此 DbContext 配置数据库提供程序.可以通过覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供程序上使用 AddDbContext 来配置提供程序.如果使用 AddDbContext,还要确保您的 DbContext 类型接受其构造函数中的 DbContextOptions 对象,并将其传递给 DbContext 的基构造函数."

"No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext."

电子数据库上下文文件

public partial class ElectronicsContext : DbContext
{
    public ElectronicsContext()
    {
    }

    public ElectronicsContext(DbContextOptions<ElectronicsContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Product> Product { get; set; }
    public virtual DbSet<ProductCategory> ProductCategory { get; set; }

Startup.cs

    var connection = @"Server=localhost;Database=Electronics;Trusted_Connection=True;ConnectRetryCount=0";
    services.AddDbContext<ElectronicsContext>(options => options.UseSqlServer(connection));

Moq Nunit 测试

 [SetUp]
 public void Setup()
 {
    var ElectronicsContext = new Mock<ElectronicsContext>();
    var ProductRepository = new Mock<ProductRepository>();

    Product producttest = new Product();
    _dbContext.Product.Add(new Product {ProductId = 1, ProductName = "TV", ProductDescription = "TV testing",ImageLocation = "test"});
    _dbContext.SaveChanges();

推荐答案

您不需要在单元测试中模拟上下文.您应该使用 DbContextOptions 类来指定要使用内存数据库来运行测试.

You don't need to mock the context in unit tests. You should use the DbContextOptions class to specify you want to use an in memory database to run your tests against.

[TestMethod]
public void TestProducts()
{
    var options = new DbContextOptionsBuilder<ElectronicsContext>()
        .UseInMemoryDatabase(databaseName: "Products Test")
        .Options;

    using(var context = new ElectronicsContext(options))
    {
        context.Products.Add(new Product {ProductId = 1, ProductName = "TV", ProductDescription = "TV testing",ImageLocation = "test"});
        context.SaveChanges();
    }

    using(var context = new ElectronicsContext(options))
    {
        // run your test here

    }
}

这针对数据库的内存表示运行,而不是依赖物理服务器.您在 startup.cs 中提供的连接字符串不用作测试的一部分.

This runs against the in-memory representation of your database instead of relying on the physical server. The connection string you provided in the startup.cs is not used as part of the tests.

可以在此处找到更多信息

这篇关于最小起订量和设置数据库上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆