示例#1
0
        public void ShouldNotUpdateModifiedOutArgument()
        {
            // Arrange
            AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithOutVoidMethod>();

            int origValue;
            var call = CallHelper.CreateCallMock(() => target.Method(out origValue));

            call.GetArguments()[0] = 42;

            sut.ResultResolver
            .ResolveResult(call)
            .Returns(
                new CallResultData(
                    Maybe.Nothing <object>(),
                    new[] { new CallResultData.ArgumentValue(0, 84) }));

            // Act
            sut.Handle(call);

            // Assert
            Assert.Equal(42, call.GetArguments()[0]);
        }
示例#2
0
        public void ShouldReturnValueFromCacheIfPresent()
        {
            // Arrange
            AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            var callSpec = Substitute.For <ICallSpecification>();

            sut.CallSpecificationFactory.CreateFrom(call, MatchArgs.AsSpecifiedInCall).Returns(callSpec);

            var cachedResult = "cachedResult";

            CallResultData ignored;

            sut.ResultCache.TryGetResult(call, out ignored)
            .Returns(c =>
            {
                c[1] = new CallResultData(Maybe.Just <object>(cachedResult),
                                          Enumerable.Empty <CallResultData.ArgumentValue>());
                return(true);
            });

            // Act
            var actualResult = sut.Handle(call);

            // Assert
            sut.ResultResolver.DidNotReceive().ResolveResult(Arg.Any <ICall>());
            Assert.True(actualResult.HasReturnValue);
            Assert.Equal(cachedResult, actualResult.ReturnValue);
        }
        public void ShouldNotUpdateModifiedRefArgument()
        {
            // Fixture setup
            AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithRefVoidMethod>();

            int origValue = 10;
            var call      = CallHelper.CreateCallMock(() => target.Method(ref origValue));

            call.GetArguments()[0] = 42;

            var callArgs = call.GetArguments();

            sut.ResultResolver
            .ResolveResult(call)
            .Returns(
                new CallResultData(
                    Maybe.Nothing <object>(),
                    new[] { new CallResultData.ArgumentValue(0, 84) }));

            // Exercise system
            sut.Handle(call);

            // Verify outcome
            Assert.Equal(42, callArgs[0]);

            // Teardown
        }
        public void ShouldNotReturnValueIfOmitted()
        {
            // Arrange
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            specimenContext.Resolve(typeof(string)).Returns(new OmitSpecimen());

            // Act
            var result = sut.ResolveResult(call);

            // Assert
            Assert.False(result.ReturnValue.HasValue());
        }
示例#5
0
        public void ShouldCacheResolvedValue()
        {
            // Arrange
            AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            var callResult =
                new CallResultData(Maybe.Nothing <object>(), Enumerable.Empty <CallResultData.ArgumentValue>());

            sut.ResultResolver.ResolveResult(call).Returns(callResult);

            // Act
            sut.Handle(call);

            // Assert
            sut.ResultCache.Received().AddResult(Arg.Any <ICallSpecification>(), callResult);
        }
        public void ShouldNotReturnValueIfOmitted()
        {
            // Fixture setup
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            specimenContext.Resolve(typeof(string)).Returns(new OmitSpecimen());

            // Exercise system
            var result = sut.ResolveResult(call);

            // Verify outcome
            Assert.False(result.ReturnValue.HasValue());

            // Teardown
        }
        public void ShouldResolveRefParameterValue()
        {
            // Arrange
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target      = Substitute.For <IInterfaceWithRefIntMethod>();
            int passedValue = 1;
            var call        = CallHelper.CreateCallMock(() => target.Method(ref passedValue));

            specimenContext.Resolve(typeof(int)).Returns(42);

            // Act
            var callResult = sut.ResolveResult(call);

            // Assert
            Assert.Single(callResult.ArgumentValues);
            Assert.Equal(0, callResult.ArgumentValues.First().Index);
            Assert.Equal(42, callResult.ArgumentValues.First().Value);
        }
        public void ShouldUseTypeRequestToResolveReturnValue()
        {
            // Arrange
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            var returnValue = "returnResult";

            specimenContext.Resolve(typeof(string)).Returns(returnValue);

            // Act
            var callResult = sut.ResolveResult(call);

            // Assert
            Assert.True(callResult.ReturnValue.HasValue());
            Assert.Equal(returnValue, callResult.ReturnValue.ValueOrDefault());
        }
        public void ShouldNotResolveArgumentValueIfOmitted()
        {
            // Arrange
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target = Substitute.For <IInterfaceWithRefIntMethod>();

            int ignored = 0;
            var call    = CallHelper.CreateCallMock(() => target.Method(ref ignored));

            specimenContext.Resolve(typeof(int)).Returns(new OmitSpecimen());

            // Act
            var result = sut.ResolveResult(call);

            // Assert
            Assert.True(result.ReturnValue.HasValue());
            Assert.Empty(result.ArgumentValues);
        }
示例#10
0
        public void ShouldUseCorrectSpecForValueCaching()
        {
            // Arrange
            AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            var callSpec = Substitute.For <ICallSpecification>();

            sut.CallSpecificationFactory.CreateFrom(call, MatchArgs.AsSpecifiedInCall).Returns(callSpec);
            sut.ResultResolver.ResolveResult(call).Returns(
                new CallResultData(Maybe.Nothing <object>(), Enumerable.Empty <CallResultData.ArgumentValue>()));

            // Act
            sut.Handle(call);

            // Assert
            sut.ResultCache.Received().AddResult(callSpec, Arg.Any <CallResultData>());
        }
示例#11
0
        public void ShouldSetOutArgumentValue()
        {
            // Arrange
            var sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithOutVoidMethod>();
            int ignored;
            var call = CallHelper.CreateCallMock(() => target.Method(out ignored));

            var callResult = new CallResultData(
                Maybe.Nothing <object>(),
                new[] { new CallResultData.ArgumentValue(0, 42) });

            sut.ResultResolver.ResolveResult(call).Returns(callResult);

            // Act
            sut.Handle(call);

            // Assert
            Assert.Equal(42, call.GetArguments()[0]);
        }
示例#12
0
        public void ShouldReturnResolvedValue()
        {
            // Arrange
            AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithParameterlessMethod>();
            var call   = CallHelper.CreateCallMock(() => target.Method());

            var callResult = "callResult";

            sut.ResultResolver.ResolveResult(call).Returns(new CallResultData(
                                                               Maybe.Just <object>(callResult),
                                                               Enumerable.Empty <CallResultData.ArgumentValue>()));

            // Act
            var actualResult = sut.Handle(call);

            // Assert
            Assert.True(actualResult.HasReturnValue);
            Assert.Equal(callResult, actualResult.ReturnValue);
        }
        public void ShouldResolveOutParameterValue()
        {
            // Fixture setup
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target = Substitute.For <IInterfaceWithParameterAndOutVoidMethod>();
            int _;
            var call = CallHelper.CreateCallMock(() => target.Method(null, out _));

            specimenContext.Resolve(typeof(int)).Returns(42);

            // Exercise system
            var callResult = sut.ResolveResult(call);

            // Verify outcome
            Assert.Single(callResult.ArgumentValues);
            Assert.Equal(1, callResult.ArgumentValues.First().Index);
            Assert.Equal(42, callResult.ArgumentValues.First().Value);

            // Teardown
        }
        public void ShouldNotResolveArgumentValueIfOmitted()
        {
            // Fixture setup
            var specimenContext = Substitute.For <ISpecimenContext>();
            var sut             = new CallResultResolver(specimenContext);

            var target = Substitute.For <IInterfaceWithRefIntMethod>();

            int _    = 0;
            var call = CallHelper.CreateCallMock(() => target.Method(ref _));

            specimenContext.Resolve(typeof(int)).Returns(new OmitSpecimen());

            // Exercise system
            var result = sut.ResolveResult(call);

            // Verify outcome
            Assert.True(result.ReturnValue.HasValue());
            Assert.Empty(result.ArgumentValues);

            // Teardown
        }
        public void ShouldSetOutArgumentValue()
        {
            // Fixture setup
            var sut = CreateSutWithMockedDependencies();

            var target = Substitute.For <IInterfaceWithOutVoidMethod>();
            int _;
            var call = CallHelper.CreateCallMock(() => target.Method(out _));

            var callResult = new CallResultData(
                Maybe.Nothing <object>(),
                new[] { new CallResultData.ArgumentValue(0, 42) });

            sut.ResultResolver.ResolveResult(call).Returns(callResult);

            // Exercise system
            sut.Handle(call);

            // Verify outcome
            Assert.Equal(42, call.GetArguments()[0]);

            // Teardown
        }