简体   繁体   English

C#Nuget或AutoCode Generator在单元测试中比较多个对象

[英]C# Nuget or AutoCode Generator to Compare Multiple Objects in Unit Testing

The following test creates error when I test tuples. 当测试元组时,以下测试会产生错误。

'Assert.AreEqual(test,productRepository.GetById(1))' threw an exception of type 'NUnit.Framework.AssertionException' 'Assert.AreEqual(test,productRepository.GetById(1))'引发了'NUnit.Framework.AssertionException'类型的异常

Many solutions presented below require an override equals function for each model. 下面介绍的许多解决方案都要求每个模型都具有覆盖等于功能。 This is very hard to maintain for software with 200+ models in project. 对于项目中具有200多个模型的软件,这很难维护。 Is there any Nuget package or auto code generator which will create equality override methods for all the 200 models? 是否有任何Nuget软件包或自动代码生成器将为所有200个模型创建相等替代方法?

These all ask to override 这些都要求覆盖

NUnit Test NUnit测试

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

    using (var context = new ElectronicsContext(options))
    {
        //DbContextOptionsBuilder<ElectronicsContext> context = new DbContextOptionsBuilder<ElectronicsContext>()

        context.Product.Add(new Product { ProductId = 1, ProductName = "TV", ProductDescription = "TV testing", ImageLocation = "test" });
        context.SaveChanges();
        ProductRepository productRepository = new ProductRepository(context);
        var test = new Product
            {ProductId = 1, ProductName = "TV", ProductDescription = "TV testing", ImageLocation = "test"};

       **//This works**
        Assert.AreEqual("TV", productRepository.GetById(1).ProductName);

       **//This Fails**
        Assert.AreEqual(test,productRepository.GetById(1));

       **//This Fails**
        Assert.AreEqual(Object.Equals(test, productRepository.GetById(1)), 1);
    }

Repository 知识库

public class ProductRepository : IProductRepository<Product>
{
    private readonly ElectronicsContext _context;
    public ProductRepository(ElectronicsContext context)
    {
        _context = context;
    }

    public IEnumerable<Product> GetAllProduct()
    {
        return _context.Product.ToList();
    }

    public IQueryable<Product> Products => _context.Product;

    public Product GetById(int productid)
    {
        return _context.Product.Find(productid);

    }
}

Model 模型

public partial class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ImageLocation { get; set; }

    public int? ProductCategoryId { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
}

You should probably be checking the Id of the object returned. 您可能应该检查返回对象的ID。 Trying to test that the classes are the same will fail if the reference pointers aren't equal. 如果引用指针不相等,则尝试测试类是否相同将失败。 Since the framework can use proxies, this will not always be true. 由于框架可以使用代理,因此并非总是如此。

Try FluentAssertions nuget package (see section " Object graph comparison "). 试用FluentAssertions nuget包(请参阅“ 对象图比较 ”部分)。

Instead of 代替

Assert.AreEqual(test,productRepository.GetById(1))

You can do something like: 您可以执行以下操作:

test.Should().BeEquivalentTo(productRepository.GetById(1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM