コード例 #1
0
        public void TwoSubscribersOneComicEach_BuildsTwoMailsEachWithOneComic()
        {
            IList <Mail> mails = null;

            using (var fakeComicFetcher = SelfInitializingFake <IComicFetcher> .For(
                       () => new WebComicFetcher(),
                       new XmlFileRecordedCallRepository("../../../RecordedCalls/TwoSubscribersOneComicEach_BuildsTwoMailsEachWithOneComic.xml")))
            {
                var target = new ComicMailBuilder(
                    DateTime.Now,
                    new SimpleConfigurationParser("[email protected]: 9chickweedlane; [email protected]: dilbert"),
                    fakeComicFetcher.Object,
                    A.Dummy <ILogger>());

                mails = target.CreateMailMessage().ToList();
            }

            mails.Should().HaveCount(2);

            mails[0].From.Address.Should().Be("*****@*****.**");
            mails[0].Personalization[0].Tos.Should().HaveCount(1);
            mails[0].Personalization[0].Tos[0].Address.Should().Be("*****@*****.**");
            mails[0].Contents[0].Value.Should().Contain(ChickweedLaneUrl);

            mails[1].From.Address.Should().Be("*****@*****.**");
            mails[1].Contents[0].Value.Should().Contain(DilbertImageUrl);
            mails[1].Personalization[0].Tos.Should().HaveCount(1);
            mails[1].Personalization[0].Tos[0].Address.Should().Be("*****@*****.**");
        }
コード例 #2
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."));
        }
コード例 #3
0
        public void FoxtrotOnSunday_MailIncludesComic()
        {
            IList <Mail> mails = null;

            var dateToCheck = MostRecent(DayOfWeek.Sunday);

            using (var fakeComicFetcher = SelfInitializingFake <IComicFetcher> .For(
                       () => new WebComicFetcher(),
                       new XmlFileRecordedCallRepository("../../../RecordedCalls/FoxTrotOnSunday.xml")))
            {
                var target = new ComicMailBuilder(
                    dateToCheck,
                    new SimpleConfigurationParser("[email protected]: foxtrot"),
                    fakeComicFetcher.Object,
                    A.Dummy <ILogger>());

                mails = target.CreateMailMessage().ToList();
            }

            mails.Should().HaveCount(1);

            mails[0].Contents[0].Value.Should()
            .NotContain("Couldn't find comic for foxtrot", "it should not have looked for the comic").And
            .NotContain($"Comic foxtrot wasn't published on {dateToCheck.ToString("dd MMMM yyyy")}.", "it should have found the comic");
        }
コード例 #4
0
        public void OneSubscriberOneComicTwiceAsFast_BuildsOneMailWithBothEpisodes()
        {
            IList <Mail> mails = null;

            using (var fakeComicFetcher = SelfInitializingFake <IComicFetcher> .For(
                       () => new WebComicFetcher(),
                       new XmlFileRecordedCallRepository("../../../RecordedCalls/OneSubscriberOneComicTwiceAsFast_BuildsOneMailWithBothEpisodes.xml")))
            {
                var now    = DateTime.Now;
                var target = new ComicMailBuilder(
                    now,
                    new SimpleConfigurationParser($"[email protected]: breaking-cat-news*2-20170327-{now.ToString("yyyyMMdd")}"),
                    fakeComicFetcher.Object,
                    A.Dummy <ILogger>());

                mails = target.CreateMailMessage().ToList();
            }

            mails.Should().HaveCount(1);

            mails[0].From.Address.Should().Be("*****@*****.**");
            mails[0].Personalization[0].Tos.Should().HaveCount(1);
            mails[0].Personalization[0].Tos[0].Address.Should().Be("*****@*****.**");
            mails[0].Contents[0].Value.Should().Contain(BreakingCatNews20170327ImageUrl);
            mails[0].Contents[0].Value.Should().Contain("alt='breaking-cat-news on 27 March 2017'");
            mails[0].Contents[0].Value.Should().Contain(BreakingCatNews20170328ImageUrl);
            mails[0].Contents[0].Value.Should().Contain("alt='breaking-cat-news on 28 March 2017'");
        }
コード例 #5
0
        public void DinosaurComicOnAWeekend_MailIndicatesComicNotPublishedToday(DayOfWeek dayOfWeek)
        {
            IList <Mail> mails = null;

            var dateToCheck = MostRecent(dayOfWeek);

            using (var fakeComicFetcher = SelfInitializingFake <IComicFetcher> .For(
                       () => new WebComicFetcher(),
                       new XmlFileRecordedCallRepository("../../../RecordedCalls/DinosaurComicsOn" + dayOfWeek + ".xml")))
            {
                var target = new ComicMailBuilder(
                    dateToCheck,
                    new SimpleConfigurationParser("[email protected]: dinosaur-comics"),
                    fakeComicFetcher.Object,
                    A.Dummy <ILogger>());

                mails = target.CreateMailMessage().ToList();
            }

            mails.Should().HaveCount(1);

            mails[0].Contents[0].Value.Should()
            .NotContain("Couldn't find comic for dinosaur-comics", "it should not have looked for the comic").And
            .Contain($"No published comic for dinosaur-comics on {dateToCheck.ToString("dd MMMM yyyy")}.", "it should tell the reader why there's no comic");
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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)]"));
        }
コード例 #10
0
        public static void CreateFromInitializedRepository(
            IRecordedCallRepository repository,
            Func <IService> serviceFactory,
            SelfInitializingFake <IService> fake)
        {
            "Given a saved call repository"
            .x(() => repository = A.Fake <IRecordedCallRepository>());

            "And the repository has been initialized"
            .x(() => A.CallTo(() => repository.Load()).Returns(Enumerable.Empty <RecordedCall>()));

            "And a service factory"
            .x(() => serviceFactory = A.Fake <Func <IService> >());

            "When I create a self-initializing fake"
            .x(() => fake = SelfInitializingFake <IService> .For(serviceFactory, repository));

            "Then the factory is not invoked"
            .x(() => A.CallTo(serviceFactory).MustNotHaveHappened());
        }
コード例 #11
0
        public static void CreateFromRepositoryAndServiceFactory(
            IRecordedCallRepository repository,
            Func <IService> serviceFactory,
            SelfInitializingFake <IService> fake)
        {
            "Given a saved call repository"
            .x(() => repository = A.Fake <IRecordedCallRepository>());

            "And a service factory"
            .x(() => serviceFactory = A.Fake <Func <IService> >());

            "When I create a self-initializing fake"
            .x(() => fake = SelfInitializingFake <IService> .For(serviceFactory, repository));

            "Then the self-initializing fake is created"
            .x(() => fake.Should().NotBeNull());

            "And its Fake property is not null"
            .x(() => fake.Object.Should().NotBeNull());
        }
コード例 #12
0
        public static void CreateFromNullCallRepository(
            IRecordedCallRepository repository,
            Func <IService> serviceFactory,
            Exception exception)
        {
            "Given a service factory"
            .x(() => serviceFactory = A.Fake <Func <IService> >());

            "And a null saved call repository"
            .x(() => repository = null);

            "When I create a self-initializing fake"
            .x(() => exception = Record.Exception(() =>
                                                  SelfInitializingFake <IService> .For(serviceFactory, repository)));

            "Then the constructor throws an exception"
            .x(() => exception.Should()
               .BeOfType <ArgumentNullException>()
               .Which.ParamName.Should().Be("repository"));
        }
コード例 #13
0
        public static void CreateFromDerivedFactoryType(
            IRecordedCallRepository repository,
            Func <Service> serviceFactory,
            SelfInitializingFake <IService> fake)
        {
            "Given a saved call repository"
            .x(() => repository = A.Fake <IRecordedCallRepository>());

            "And the repository has not been initialized"
            .x(() => A.CallTo(() => repository.Load()).Returns(null));

            "And a service factory that creates a derived type"
            .x(() => serviceFactory = A.Fake <Func <Service> >());

            "When I create a self-initializing fake"
            .x(() => fake = SelfInitializingFake <IService> .For <IService>(serviceFactory, repository));

            "Then the factory is invoked to create the service"
            .x(() => A.CallTo(serviceFactory).MustHaveHappened());
        }
コード例 #14
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));
        }
コード例 #15
0
        public void SubscribesToComicsKingdomComics_BuildsOneMailWithBothComics()
        {
            IList <Mail> mails = null;

            using (var fakeComicFetcher = SelfInitializingFake <IComicFetcher> .For(
                       () => new WebComicFetcher(),
                       new XmlFileRecordedCallRepository("../../../RecordedCalls/SubscribesToComicsKingdomComics_BuildsOneMailWithBothComics.xml")))
            {
                var target = new ComicMailBuilder(
                    new DateTime(2019, 7, 14),
                    new SimpleConfigurationParser("[email protected]: blondie, rhymes-with-orange"),
                    fakeComicFetcher.Object,
                    A.Dummy <ILogger>());

                mails = target.CreateMailMessage().ToList();
            }

            mails.Should().HaveCount(1);

            mails[0].Contents[0].Value.Should()
            .Contain(BlondieUrl, "it should have Blondie").And
            .Contain(RhymesWithOrangeUrl, "it should have Rhymes with Orange");
        }
コード例 #16
0
        public static void CalvinAndHobbesOnSunday_MailIncludesComic()
        {
            IList <Mail> mails = null;

            using (var fakeComicFetcher = SelfInitializingFake <IComicFetcher> .For(
                       () => new WebComicFetcher(),
                       new XmlFileRecordedCallRepository("../../../RecordedCalls/CalvinAndHobbesOnSunday.xml")))
            {
                var dateToCheck = MostRecent(DayOfWeek.Sunday);
                var target      = new ComicMailBuilder(
                    dateToCheck,
                    new ConfigurationParser("[email protected]: calvinandhobbes"),
                    fakeComicFetcher.Object,
                    A.Dummy <ILogger>());

                mails = target.CreateMailMessage().ToList();
            }

            mails.Should().HaveCount(1);

            mails[0].Contents[0].Value.Should()
            .NotContain("Couldn't find comic for calvinandhobbes", "it should not have looked for the comic").And
            .Contain(CalvinAndHobbesSundayUrl, "it should have found the comic");
        }
コード例 #17
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());
        }
コード例 #18
0
        public static void SerializeVoidCall(
            string path,
            IRecordedCallRepository repository,
            IService realServiceWhileRecording,
            int voidMethodOutInteger,
            DateTime voidMethodRefDateTime,
            Guid nonVoidMethodResult)
        {
            "Given a file path"
            .x(() => path = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".xml"));

            "And a XmlFileRecordedCallRepository targeting that path"
            .x(() => repository = new XmlFileRecordedCallRepository(path));

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

                int i;
                DateTime dt = DateTime.MinValue;
                A.CallTo(() => realServiceWhileRecording.VoidMethod("firstCallKey", out i, ref dt))
                .AssignsOutAndRefParameters(17, new DateTime(2017, 1, 24));

                A.CallTo(() => realServiceWhileRecording.NonVoidMethod())
                .Returns(new Guid("6c7d8912-802a-43c0-82a2-cb811058a9bd"));
            });

            "When I use a self-initializing fake in recording mode"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <IService> .For(() => realServiceWhileRecording, repository))
                {
                    var fake = fakeService.Object;
                    fake.VoidMethod("firstCallKey", out voidMethodOutInteger, ref voidMethodRefDateTime);
                    nonVoidMethodResult = fake.NonVoidMethod();
                }
            });

            "And I use a self-initializing fake in playback mode"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <IService> .For <IService>(UnusedFactory, repository))
                {
                    var fake = playbackFakeService.Object;
                    int i;
                    DateTime dt = DateTime.MinValue;
                    fake.VoidMethod("blah", out i, ref dt);
                }
            });

            "Then the recording fake forwards calls to the wrapped service"
            .x(() =>
            {
                int i;
#if BUG_ASSIGNING_REF_VALUE_CLEARS_INCOMING_VALUE
                DateTime dt = new DateTime(2017, 1, 24);
#else
                DateTime dt = DateTime.MinValue;
#endif
                A.CallTo(() => realServiceWhileRecording.VoidMethod(A <string> ._, out i, ref dt))
                .MustHaveHappened();
                A.CallTo(() => realServiceWhileRecording.NonVoidMethod()).MustHaveHappened();
            });

            "And the playback fake returns the recorded out and ref parameters and results"
            .x(() =>
            {
                voidMethodOutInteger.Should().Be(17);
                voidMethodRefDateTime.Should().Be(new DateTime(2017, 1, 24));
                nonVoidMethodResult.Should().Be(new Guid("6c7d8912-802a-43c0-82a2-cb811058a9bd"));
            });
        }
コード例 #19
0
        public static void SerializeVoidCall(
            string path,
            IRecordedCallRepository repository,
            IService realServiceWhileRecording,
            int voidMethodOutInteger,
            DateTime voidMethodRefDateTime,
            IDictionary <string, Guid> nonVoidMethodResult)
        {
            "Given a file path"
            .x(() => path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));

            "And a BinaryFileRecordedCallRepository targeting that path"
            .x(() => repository = new BinaryFileRecordedCallRepository(path));

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

                int i;
                DateTime dt = DateTime.MinValue;
                A.CallTo(() => realServiceWhileRecording.VoidMethod("firstCallKey", out i, ref dt))
                .AssignsOutAndRefParameters(17, new DateTime(2017, 1, 24));

                A.CallTo(() => realServiceWhileRecording.NonVoidMethod())
                .Returns(new Dictionary <string, Guid>
                {
                    ["key1"] = new Guid("6c7d8912-802a-43c0-82a2-cb811058a9bd"),
                });
            });

            "When I use a self-initializing fake in recording mode"
            .x(() =>
            {
                using (var fakeService = SelfInitializingFake <IService> .For(() => realServiceWhileRecording, repository))
                {
                    var fake = fakeService.Object;
                    fake.VoidMethod("firstCallKey", out voidMethodOutInteger, ref voidMethodRefDateTime);
                    nonVoidMethodResult = fake.NonVoidMethod();
                }
            });

            "And I use a self-initializing fake in playback mode"
            .x(() =>
            {
                using (var playbackFakeService = SelfInitializingFake <IService> .For <IService>(() => null, repository))
                {
                    var fake = playbackFakeService.Object;
                    int i;
                    DateTime dt = DateTime.MinValue;
                    fake.VoidMethod("blah", out i, ref dt);
                }
            });

            "Then the recording fake forwards calls to the wrapped service"
            .x(() =>
            {
                int i;
#if FAKEITEASY3
                DateTime dt = new DateTime(2017, 1, 24);
#else
                DateTime dt = DateTime.MinValue;
#endif
                A.CallTo(() => realServiceWhileRecording.VoidMethod(A <string> ._, out i, ref dt))
                .MustHaveHappened();
                A.CallTo(() => realServiceWhileRecording.NonVoidMethod()).MustHaveHappened();
            });

            "And the playback fake returns the recorded out and ref parameters and results"
            .x(() =>
            {
                voidMethodOutInteger.Should().Be(17);
                voidMethodRefDateTime.Should().Be(new DateTime(2017, 1, 24));
                nonVoidMethodResult.Should()
                .HaveCount(1).And
                .ContainKey("key1")
                .WhichValue.Should().Be(new Guid("6c7d8912-802a-43c0-82a2-cb811058a9bd"));
            });
        }