0

The following test creates error when I test tuples.

'Assert.AreEqual(test,productRepository.GetById(1))' threw an exception of type 'NUnit.Framework.AssertionException'

How would I resolve this? Is this due to some comparison issue when I test var tuples? Note the following other test that works:

The other comparison works on just single model lines.

NUnit Test

[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));

    }

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; }
}

1 Answers1

1

To check for equality of two objects you need to override Equals method

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; }

    public override bool Equals(object obj)
    {
        if ((obj == null) || ! this.GetType().Equals(obj.GetType()))
        {
            return false;
        }

        Product other = (Product)obj;
        return ProductId == other.ProductId && ProductName.Equals(other.ProductName); // or anything else you want to compare
    }
}
Guy
  • 46,488
  • 10
  • 44
  • 88
  • @HardyWest You can build a method that compares individual fields of two `Product`s, but it will looks exactly like the overridden `Equals` method. There is no problem with overriding it. – Guy Oct 17 '18 at 04:54
  • ok, so I will override, and I assume overriding will not have any negative side consequences, Thanks, is this the standard software development approach? Do most people conduct this method? –  Oct 17 '18 at 04:55
  • @HardyWest You can look at the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.object.equals?view=netframework-4.7.2) for more details. – Guy Oct 17 '18 at 04:57
  • @HardyWest Not that I know of. – Guy Oct 17 '18 at 04:58
  • @HardyWest Did you override `Equals()`? if not, you can't check for equality in any way. – Guy Oct 18 '18 at 04:42
  • thanks it works, I will also look into IEquatable now –  Oct 23 '18 at 03:59
  • A remark only remotely related to the answer: if you override Equals, it is advisable to also override GetHashCode. https://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden – Hermann.Gruber Nov 06 '18 at 20:39