PapGroup / NSubstitute.FluentAssertionsBridge

a library for bridging between NSubstitute and FluentAssertions
MIT License
10 stars 2 forks source link

.ReceivedWithAnyArgs breaks tests/exceptions #2

Closed rklec closed 1 year ago

rklec commented 1 year ago

STR

This works as expected:


        [Test]
        public void Fails_WhenNotCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.Received(1)
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }

        [Test]
        public void Fails_WhenWronglyCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.Register(new RegistrationModel() { Username = "Admin", Password = "but wrong password" });

            service.Received(1)
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }

        [Test]
        public void Works_WhenCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.Register(expected);

            service.Received(1)
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }
        public class RegistrationModel
        {
            public string Username { get; set; }

            public string Password { get; set; }
        }

        public interface IRegistrationService
        {
            void Register(RegistrationModel that);
        }

But when you use .ReceivedWithAnyArgs it suddenly fails:

        [Test]
        public void Fails_WhenNotCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.ReceivedWithAnyArgs(1)
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }

        [Test]
        public void Fails_WhenWronglyCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.Register(new RegistrationModel() { Username = "Admin", Password = "but wrong password" });

            service.ReceivedWithAnyArgs(1)
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }

        [Test]
        public void Works_WhenCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.Register(expected);

            service.ReceivedWithAnyArgs(1)
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }

        public class RegistrationModel
        {
            public string Username { get; set; }

            public string Password { get; set; }
        }

        public interface IRegistrationService
        {
            void Register(RegistrationModel that);
        }    

What happens

All tests work, except the one in the middle, this now suddenly succeeds, although it should fail: grafik

What should happen

The test should fail, as before.

System

FluentAssertionsBridge v 1.0.0 NSubstitute 4.4.0

Notes

If I reverse the behaviour that is also interesting:

[Test]
        public void ShouldWorkNowAsICheckForCorrectPasswordNOTBeingReceived_WhenWronglyCalled()
        {
            var service = Substitute.For<IRegistrationService>();
            var expected = new RegistrationModel() { Username = "Admin", Password = "123456" };

            service.Register(new RegistrationModel() { Username = "Admin", Password = "but wrong password" });

            service.DidNotReceiveWithAnyArgs()
                .Register(Verify.That<RegistrationModel>(a => a.Should().BeEquivalentTo(expected)));
        }     

This fails with:

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive no calls matching:
    Register(any DienstausweisControllerTest+ExportAsLogaExcelFile+RegistrationModel)
Actually received 1 matching call:
    Register(DienstausweisControllerTest+ExportAsLogaExcelFile+RegistrationModel)

   at NSubstitute.Core.ReceivedCallsExceptionThrower.Throw(ICallSpecification callSpecification, IEnumerable`1 matchingCalls, IEnumerable`1 nonMatchingCalls, Quantity requiredQuantity)
   at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call)
   at NSubstitute.Routing.Route.Handle(ICall call)
   at NSubstitute.Core.CallRouter.Route(ICall call)
   at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at NSubstitute.Proxies.CastleDynamicProxy.ProxyIdInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.ObjectProxy_18.Register(RegistrationModel that)

which is also wrong.

rklec commented 1 year ago

Uff okay no, any argumentas of course means the arguments are ignored. ignore that, sorry