Coder Social home page Coder Social logo

nsubstitute_xunitequivalence's Introduction

NSubstitute XUnitEquivalence

Motivation

Can't do deep equivalency checks with NSubstitute...

Comparison with Arg.Is

Before

var dependency = Substitute.For<IDependency>();
var sut = new Sut(dependency);

sut.Call();

dependency.Received(1).Call(Arg.Is<Argument>(p =>
    p.Id == 1 &&
    p.SubArgs.Length == 2 &&
    p.SubArgs[0].Id == 2 &&
    p.SubArgs[1].Id == 3
));

/// Message: 
/// NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching:
/// 	Call(p => ((((p.Id == 1) AndAlso (ArrayLength(p.SubArgs) == 2)) AndAlso (p.SubArgs[0].Id == 2)) AndAlso (p.SubArgs[1].Id == 3)))
/// Actually received no matching calls.
/// Received 1 non-matching call (non-matching arguments indicated with '*' characters):
/// 	Call(*Argument*)

After

var dependency = Substitute.For<IDependency>();
var sut = new Sut(dependency);

sut.Call();

dependency.Received(1).Call(ArgExt.IsEquivalentTo(new Argument
{
    Id = 1,
    SubArgs = new SubArgument[]
    {
        new()
        {
            Id = 2
        },
        new()
        {
            Id = 3
        }
    }
}));

/// Message: 
/// NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching:
/// 	Call(NSubstitute.Core.Arguments.ArgumentMatcher+GenericToNonGenericMatcherProxyWithDescribe`1[NSubstitute_XUnitEquivalence.Argument])
/// Actually received no matching calls.
/// Received 1 non-matching call (non-matching arguments indicated with '*' characters):
/// 	Call(*Argument*)
/// 		arg[0]: Assert.Equivalent() Failure: Collection value not found in member 'SubArgs'
/// 		        Expected: SubArgument { Id = 2 }
/// 		        In:       [SubArgument { Id = 3 }]

Conclusion

Admit it, with Arg.Is<T> you can't see what's wrong when your test fails, because it just checks whether the boolean is true/false.
ArgExt.IsEquivalentTo uses XUnit's Assert.Equivalent() under the hood, and passes through a more detailed error message.

Assert.Equivalent GitHub Issue

From the docs:

Assert.Equivalent differs from Assert.Equal is the “level of equality” that is expected from the two values. For example, Assert.Equal requires that both values are the same (or a compatible) type, whereas Assert.Equivalent will simply compare all the public fields and properties of the two values to ensure they contain the same values, even if they aren’t the same type. Equivalence comes with a “strictness” switch which allows the developer to say whether the expected value contains the complete set of values that the actual value should contain (‘strict’) vs. only a subset of values (‘not strict’). When strict comparisons are done, an “extra” properties on the actual object vs. the expected object cause failure, whereas they are ignored for non-strict comparisons.

Forget Arg.Is<T> and just use ArgExt.IsEquivalentTo for everything.
It works for value types, reference types, collections, dictionaries, whatever...
Also much easier to read and write when doing Test Driven Design.

Prove me wrong.

nsubstitute_xunitequivalence's People

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.