Giter Site home page Giter Site logo

Comments (4)

dtchepak avatar dtchepak commented on June 8, 2024

Hi @hgirma ,

Is equality implemented for this type? Or is it using reference equality?
e.g. if you new up two instances and Assert.That(a, Is.EqualTo(b)) does it pass when NSubstitute is not involved?

from nsubstitute.

hgirma avatar hgirma commented on June 8, 2024

Hi @dtchepak

No it does not, I created a sample repo to reproduce the issue

https://github.com/hgirma/WebApplication2

similar setup work with other types in my codebase and without IEquatable implemented.

from nsubstitute.

Ergamon avatar Ergamon commented on June 8, 2024

I also waited for an example, because I expected the same. Many of our junior developers make the same mistake. We humans always think that 2 objects having the same information must be the same. Sadly that is not what .NET does. If you dont change anything about your objects, then Equals only returns true if they are the same object.

This has nothing to do with NSubstitute, but is how .NET works:

      // Assert
      user.Should().Be(user);
      transaction.Should().Be(new Transaction
      {
        Amount = -shopItem.Amount,
        CreatedBy = user.Id,
        CurrencyId = shopItem.CostCurrencyTypeId,
        CurrencyType = shopItem.CostCurrencyType,
        Note = $"Redeemed: {shopItem.Description}",
        UserId = user.Id,
        OpportunityType = "Redeem",
        TransactionKind = TransactionKind.Shop,
        CreatedDate = _timeProvider.GetUtcNow().LocalDateTime,
        IsDeleted = false,
      });

This is basically the same test and fails exactly the same way.

There are many ways to solve this:

  • Write a predicate comparing property for property, like you did in your working test
  • Let your classes implement IEquitable, basically overriding Equals
  • Use records where the SourceGenerator does this for you
  • Combine NSubstitute with FluentAssertions and use BeEquivalentTo instead of Be

from nsubstitute.

hgirma avatar hgirma commented on June 8, 2024

yep, junior mistake :) I went with predicate for now...

from nsubstitute.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.