예제 #1
0
        public void lazy_should_invoke_only_once()
        {
            var numberOfInvocations = 0;
            var lazyHeavyClass      = new Lazy <HeavyClass>(() =>
            {
                numberOfInvocations++;
                return(new HeavyClass());
            });

            var sut = new LightClassWithLazy(lazyHeavyClass);

            sut.SomeMethod();
            sut.SomeMethod();
            sut.SomeMethod();

            numberOfInvocations.Should().Be(1);
        }
예제 #2
0
        public void should_defer_the_creation_using_Lazy()
        {
            var created        = false;
            var lazyHeavyClass = new Lazy <HeavyClass>(() =>
            {
                created = true;
                return(new HeavyClass());
            });

            var sut = new LightClassWithLazy(lazyHeavyClass);

            created.Should().Be(false);

            sut.SomeMethod();

            created.Should().Be(true);
        }