Ejemplo n.º 1
0
        public static void CannotRecordNonVoidAfterDisposing(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            IService realServiceWhileRecording,
            SelfInitializingFake <IService> fakeService,
            Exception exception)
        {
            "Given a call storage object"
            .x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() => realServiceWhileRecording = A.Fake <IService>());

            "And a self-initializing fake wrapping the service"
            .x(() => fakeService = SelfInitializingFake <IService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository));

            "And the fake is disposed"
            .x(() => fakeService.Dispose());

            "When I record a call to a non-void method using the fake"
            .x(() => exception = Record.Exception(() => fakeService.Object.Function()));

            "Then the fake throws an exception"
            .x(() => exception.Should()
               .BeOfType <RecordingException>()
               .Which.Message.Should().Be("The fake has been disposed and can record no more calls."));
        }
        public static void IgnoresParameterValuesInRecordedCalls(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            ILibraryService realServiceWhileRecording,
            IEnumerable <int> countsWhileRecording,
            IEnumerable <int> countsDuringPlayback)
        {
            "Given a call storage object"
            .x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() =>
            {
                realServiceWhileRecording = A.Fake <ILibraryService>();

                A.CallTo(() => realServiceWhileRecording.GetCount("1"))
                .Returns(0x1);
                A.CallTo(() => realServiceWhileRecording.GetCount("2"))
                .Returns(0x2);
            });

            "When I use a self-initializing fake in recording mode to get the counts for book 2 and 1"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <ILibraryService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository))
                {
                    var fake = fakeService.Object;

                    countsWhileRecording = new List <int>
                    {
                        fake.GetCount("2"),
                        fake.GetCount("1"),
                    };
                }
            });

            "And I use a self-initializing fake in playback mode to get the counts for book 1 and 2"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <ILibraryService> .For(UnusedFactory, inMemoryRecordedCallRepository))
                {
                    var fake = playbackFakeService.Object;

                    countsDuringPlayback = new List <int>
                    {
                        fake.GetCount("1"),
                        fake.GetCount("2"),
                    };
                }
            });

            "Then the recording fake returns the wrapped service's results"
            .x(() => countsWhileRecording.Should().Equal(0x2, 0x1));

            // These results demonstrate that the self-initializing fake relies on a script
            // defined by which methods are called, without regard to the arguments
            // passed to the methods.
            "And the playback fake returns results in 'recorded order'"
            .x(() => countsDuringPlayback.Should().Equal(0x2, 0x1));
        }
        public static void ReplaysRecordedCalls(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            ILibraryService realServiceWhileRecording,
            IEnumerable <int> countsWhileRecording,
            IEnumerable <int> countsDuringPlayback)
        {
            "Given a call storage object".x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() =>
            {
                realServiceWhileRecording = A.Fake <ILibraryService>();

                A.CallTo(() => realServiceWhileRecording.GetCount("1"))
                .ReturnsNextFromSequence(0x1A, 0x1B);
                A.CallTo(() => realServiceWhileRecording.GetCount("2"))
                .Returns(0x2);
            });

            "When I use a self-initializing fake in recording mode to get the counts for book 1, 2, and 1 again"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <ILibraryService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository))
                {
                    var fake             = fakeService.Object;
                    countsWhileRecording = new List <int>
                    {
                        fake.GetCount("1"),
                        fake.GetCount("2"),
                        fake.GetCount("1"),
                    };
                }
            });

            "And I use a self-initializing fake in playback mode to get the counts for book 1, 2, and 1 again"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <ILibraryService> .For(UnusedFactory, inMemoryRecordedCallRepository))
                {
                    var fake             = playbackFakeService.Object;
                    countsDuringPlayback = new List <int>
                    {
                        fake.GetCount("1"),
                        fake.GetCount("2"),
                        fake.GetCount("1"),
                    };
                }
            });

            "Then the recording fake forwards calls to the wrapped service"
            .x(() => A.CallTo(() => realServiceWhileRecording.GetCount("1"))
               .MustHaveHappenedTwiceExactly());

            "And the recording fake returns the wrapped service's results"
            .x(() => countsWhileRecording.Should().Equal(0x1A, 0x2, 0x1B));

            "And the playback fake returns the recorded results"
            .x(() => countsDuringPlayback.Should().Equal(0x1A, 0x2, 0x1B));
        }
        public static void VoidOutAndRef(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            IService realServiceWhileRecording,
            int recordingOut,
            int recordingRef,
            int playbackOut,
            int playbackRef)
        {
            "Given a call storage object"
            .x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() =>
            {
                realServiceWhileRecording = A.Fake <IService>();

                int localOut;
                int localRef = 0;
                A.CallTo(() => realServiceWhileRecording.SetSomeOutAndRefParameters(out localOut, ref localRef))
                .WithAnyArguments()
                .AssignsOutAndRefParameters(7, -14);
            });

            "When I use a self-initializing fake in recording mode to set some out and ref parameters"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <IService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository))
                {
                    var fake = fakeService.Object;
                    fake.SetSomeOutAndRefParameters(out recordingOut, ref recordingRef);
                }
            });

            "And I use a self-initializing fake in playback mode to set some out and ref parameters"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <IService> .For <IService>(() => null, inMemoryRecordedCallRepository))
                {
                    var fake = playbackFakeService.Object;
                    fake.SetSomeOutAndRefParameters(out playbackOut, ref playbackRef);
                }
            });

            "Then the recording fake sets the out parameter to the value set by the wrapped service"
            .x(() => recordingOut.Should().Be(7));

            "And it sets the ref parameter to the value set by the wrapped service"
            .x(() => recordingRef.Should().Be(-14));

            "Then the playback fake sets the out parameter to the value seen in recording mode"
            .x(() => playbackOut.Should().Be(7));

            "And it sets the ref parameter to the value seen in recording mode"
            .x(() => playbackRef.Should().Be(-14));
        }
        public static void ThrowsIfTooManyCallsEncountered(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            ILibraryService realServiceWhileRecording,
            Exception exception)
        {
            "Given a call storage object"
            .x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() => realServiceWhileRecording = A.Fake <ILibraryService>());

            "When I use a self-initializing fake in recording mode to get the count and title for book 1"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <ILibraryService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository))
                {
                    var fake = fakeService.Object;

                    fake.GetCount("1");
                    fake.GetTitle("1");
                }
            });

            "And I use a self-initializing fake in playback mode to get the count and title and count for book 1"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <ILibraryService> .For(UnusedFactory, inMemoryRecordedCallRepository))
                {
                    var fake = playbackFakeService.Object;

                    fake.GetCount("1");
                    fake.GetTitle("1");
                    exception = Record.Exception(() => fake.GetCount("1"));
                }
            });

            // This result demonstrates that the self-initializing fake relies on a script
            // defined by which methods are called, and is completely inflexible with
            // regard to the number of repetitions of the calls.
            "Then the playback fake throws a playback exception"
            .x(() => exception.Should()
               .BeOfType <PlaybackException>().Which.Message.Should()
               .Be("expected no more calls, but found [Int32 GetCount(System.String)]"));
        }
Ejemplo n.º 6
0
        public static void VoidMethodThrowsExceptionWhileRecording(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            IService realServiceWhileRecording,
            Exception originalException,
            Exception exceptionWhileRecording,
            Exception exceptionWhileEndingRecordingSession)
        {
            "Given a call storage object"
            .x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() => realServiceWhileRecording = A.Fake <IService>());

            "And the real service throws an exception when executing a void method"
            .x(() =>
            {
                A.CallTo(() => realServiceWhileRecording.Action())
                .Throws(originalException = new InvalidOperationException());
            });

            "When I use a self-initializing fake in recording mode to execute the method"
            .x(() =>
            {
                var fakeService = SelfInitializingFake <IService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository);
                var fake        = fakeService.Object;

                exceptionWhileRecording = Record.Exception(() => fake.Action());

                exceptionWhileEndingRecordingSession = Record.Exception(() => fakeService.Dispose());
            });

            "Then the recording fake throws the original exception"
            .x(() => exceptionWhileRecording.Should().BeSameAs(originalException));

            "But ending the recording session throws a recording exception"
            .x(() => exceptionWhileEndingRecordingSession.Should().BeOfType <RecordingException>()
               .Which.Message.Should().Be("error encountered while recording actual service calls"));

            "And the session-ending exception has the original exception as its inner exception"
            .x(() => exceptionWhileEndingRecordingSession.InnerException.Should().BeSameAs(originalException));
        }
Ejemplo n.º 7
0
        public static void NonVoidOutAndRef(
            InMemoryRecordedCallRepository inMemoryRecordedCallRepository,
            IService realServiceWhileRecording,
            int recordingOut,
            int recordingRef,
            bool recordingReturn,
            int playbackOut,
            int playbackRef,
            bool playbackReturn)
        {
            "Given a call storage object"
            .x(() => inMemoryRecordedCallRepository = new InMemoryRecordedCallRepository());

            "And a real service to wrap while recording"
            .x(() =>
            {
                realServiceWhileRecording = A.Fake <IService>();

                int localOut;
                int localRef = 0;
                A.CallTo(() => realServiceWhileRecording.TryToSetSomeOutAndRefParameters(out localOut, ref localRef))
                .WithAnyArguments()
                .Returns(true)
                .AssignsOutAndRefParameters(19, 8);
            });

            "When I use a self-initializing fake in recording mode to try to set some out and ref parameters"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <IService> .For(() => realServiceWhileRecording, inMemoryRecordedCallRepository))
                {
                    var fake        = fakeService.Object;
                    recordingReturn = fake.TryToSetSomeOutAndRefParameters(out recordingOut, ref recordingRef);
                }
            });

            "And I use a self-initializing fake in playback mode to try to set some out and ref parameters"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <IService> .For <IService>(UnusedFactory, inMemoryRecordedCallRepository))
                {
                    var fake       = playbackFakeService.Object;
                    playbackReturn = fake.TryToSetSomeOutAndRefParameters(out playbackOut, ref playbackRef);
                }
            });

            "Then the recording fake sets the out parameter to the value set by the wrapped service"
            .x(() => recordingOut.Should().Be(19));

            "And it sets the ref parameter to the value set by the wrapped service"
            .x(() => recordingRef.Should().Be(8));

            "And it returns the value that the wrapped service did"
            .x(() => recordingReturn.Should().BeTrue());

            "Then the playback fake sets the out parameter to the value seen in recording mode"
            .x(() => playbackOut.Should().Be(19));

            "And it sets the ref parameter to the value seen in recording mode"
            .x(() => playbackRef.Should().Be(8));

            "And it returns the value seen in recording mode"
            .x(() => playbackReturn.Should().BeTrue());
        }