Ejemplo n.º 1
0
        public void Reset_Should_Replace_Default()
        {
            var defaultCount      = new InstanceCounter();
            var simpleLazyFactory = SimpleLazyFactory <IFooService> .CreateForTest();

            var defaultFunc = simpleLazyFactory.Default(() => new FooService(defaultCount)).Resolve;

            var instanceCount = new InstanceCounter();
            var theOne        = new FooService(instanceCount);
            var resetFunc     = simpleLazyFactory.Reset(() => theOne).Resolve;

            var fooService  = defaultFunc();
            var fooService2 = resetFunc();

            fooService.ShouldEqual(fooService2);

            for (int i = 0; i < 10; i++)
            {
                defaultFunc();
            }

            for (int i = 0; i < 10; i++)
            {
                resetFunc();
            }

            defaultCount.TotalCount.ShouldEqual(0);
            instanceCount.TotalCount.ShouldEqual(1);
        }
Ejemplo n.º 2
0
 public void Default_Not_Init_Should_Ex()
 {
     AssertHelper.ShouldThrows <Exception>(() =>
     {
         var simpleLazyFactory = SimpleLazyFactory <IFooService> .CreateForTest();
         simpleLazyFactory.Resolve();
     });
 }
Ejemplo n.º 3
0
 public void Default_Multi_Call_Should_Ex()
 {
     AssertHelper.ShouldThrows <Exception>(() =>
     {
         var defaultCount      = new InstanceCounter();
         var simpleLazyFactory = SimpleLazyFactory <IFooService> .CreateForTest();
         simpleLazyFactory.Default(() => new FooService(defaultCount));
         simpleLazyFactory.Default(() => new FooService(defaultCount));
     });
 }
Ejemplo n.º 4
0
        public void Default_Multi_Call_Should_Return_Same()
        {
            var defaultCount      = new InstanceCounter();
            var simpleLazyFactory = SimpleLazyFactory <IFooService> .CreateForTest();

            var defaultFunc = simpleLazyFactory.Default(() => new FooService(defaultCount)).Resolve;
            var fooService  = defaultFunc();
            var fooService2 = defaultFunc();

            defaultCount.TotalCount.ShouldEqual(1);
            fooService.ShouldEqual(fooService2);
        }
Ejemplo n.º 5
0
        public void Default_Multi_Call_Should_Singleton()
        {
            var defaultCount      = new InstanceCounter();
            var simpleLazyFactory = SimpleLazyFactory <IFooService> .CreateForTest();

            var defaultFunc = simpleLazyFactory.Default(() => new FooService(defaultCount)).Resolve;

            for (int i = 0; i < 10; i++)
            {
                defaultFunc();
            }
            defaultCount.TotalCount.ShouldEqual(1);
        }
Ejemplo n.º 6
0
        public void Reset_Multi_Time_Should_Ok()
        {
            var counter1          = new InstanceCounter();
            var counter2          = new InstanceCounter();
            var simpleLazyFactory = SimpleLazyFactory <IFooService> .CreateForTest();

            simpleLazyFactory.Reset(() => new FooService(counter1));
            simpleLazyFactory.Reset(() => new FooService(counter2));
            var resolve = simpleLazyFactory.Resolve;

            for (int i = 0; i < 3; i++)
            {
                var service = resolve();
            }
            counter1.TotalCount.ShouldEqual(0);
            counter2.TotalCount.ShouldEqual(1);
        }