示例#1
0
        public void AddSpecificIsNotWeak()
        {
            var collection = new MauiServiceCollection();
            var services   = new MauiFactory(collection);
            var context    = new MauiContext(services);

            DoAdd(context);

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.NotNull(context.Services.GetService <TestThing>());
示例#2
0
        public void CloneIncludeSameServices()
        {
            var obj = new TestThing();

            var collection = new MauiServiceCollection();

            collection.AddSingleton(obj);
            var services = new MauiFactory(collection);

            var first  = new MauiContext(services);
            var second = new MauiContext(first.Services);

            Assert.Same(obj, second.Services.GetService <TestThing>());
        }
示例#3
0
        public void AddSpecificInstanceOverridesBase()
        {
            var baseObj = new TestThing();

            var collection = new MauiServiceCollection();

            collection.AddSingleton(baseObj);
            var services = new MauiFactory(collection);

            var specificObj = new TestThing();
            var context     = new MauiContext(services);

            context.AddSpecific <TestThing>(specificObj);

            Assert.Same(specificObj, context.Services.GetService <TestThing>());
        }
示例#4
0
        public void GetRequiredServiceRetrievesService()
        {
            HandlerStub handlerStub = new HandlerStub();

            var collection = new MauiServiceCollection();

            collection.TryAddSingleton <IMauiHandlersFactory>(new MauiHandlersFactory(null));
            collection.TryAddSingleton <IFooService, FooService>();

            var provider = new MauiFactory(collection);

            handlerStub.SetMauiContext(new HandlersContextStub(provider));

            Assert.NotNull(handlerStub.MauiContext);
            Assert.NotNull(handlerStub.MauiContext.Services);

            var foo = handlerStub.GetRequiredService <IFooService>();

            Assert.IsType <FooService>(foo);
        }