Beispiel #1
0
        public void GetRequiredServiceThrowsOnNoContext()
        {
            HandlerStub handlerStub = new HandlerStub();

            Assert.Null((handlerStub as IViewHandler).MauiContext);

            var ex = Assert.Throws <InvalidOperationException>(() => handlerStub.GetRequiredService <IFooService>());

            Assert.Contains("the context", ex.Message);
            Assert.Contains("MauiContext", ex.Message);
        }
Beispiel #2
0
        public void GetRequiredServiceThrowsOnNoServices()
        {
            HandlerStub handlerStub = new HandlerStub();

            handlerStub.SetMauiContext(new InvalidHandlersContextStub());

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

            var ex = Assert.Throws <InvalidOperationException>(() => handlerStub.GetRequiredService <IFooService>());

            Assert.Contains("the service provider", ex.Message);
            Assert.Contains("MauiContext", ex.Message);
        }
Beispiel #3
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);
        }