【问题标题】:C# Nunit and Running same database populate everytime before test每次测试前都会填充 C# Nunit 和运行相同的数据库
【发布时间】:2018-10-18 05:49:55
【问题描述】:

我有一个实体框架数据库上下文文件。 我将如何设置 DBContext,并在运行测试之前每次运行 SetUp(以拥有干净的填充数据库)?它在 NUnit 测试中看不到 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; }

Nunit 设置

public class TestClass
{

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

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

此处出现错误消息

The name 'context' does not exist in the current context    

    [Test]
    public void TestProductRepository()
    {
        ProductRepository productRepository = new ProductRepository(context);
        Assert.AreEqual("TV", productRepository.GetById(1).ProductName);
    }
}

SetUp 的替代想法也不起作用:

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

        ElectronicsContext context = new ElectronicsContext(options);

【问题讨论】:

  • 虽然已经有两个人回答了,但是如果不知道你使用的NUnit的版本,至少是V2还是V3,实际上是不可能给你的问题一个好的答案的。 :-(我可以写两个答案,但我很懒。:-)

标签: c# entity-framework asp.net-core nunit


【解决方案1】:

创建测试类的私有成员,可以在SetUp方法中实例化并在测试方法中使用。

因为 NUnit 会为类中的所有测试创建一次 TestClass 的实例,所以不能使用 readonly 成员。 应该为每个测试重新分配(重置)上下文,以使它们彼此隔离。

public class TestClass
{
    private DbContext _context;

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

        // new instance of ElectronicsContext will be created for every test.
        _context = new ElectronicsContext(options);

        // Use _context to insert initial data required for the test

    }

    [TearDown]
    public void TearDown()
    {
        _context.Dispose();
    }

    [Test]
    public void RunTest()
    {
        // run test
        // assert
        _context.Products.Count().Should().Be(10)
    }
}

想象一下,测试夹具只是一个 c# 类,其中标有 Test 属性的方法按顺序执行。

var test = new TestClass();
// first test
test.SetUp();    // create db context
test.RunTest();  // use db context
test.TearDown(); // dispose db context

// second test
test.SetUp();       // create db context
test.RunTest_Two(); // use db context
test.TearDown();    // dispose db context

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多