示例#1
0
        public void CreateActionInstanceTest()
        {
            var actionInfo = ActionActivator.GetActionInfo(typeof(ActionWithImports));

            var serviceProvider = A.Fake <IServiceProvider>();

            A.CallTo(() => serviceProvider.GetService(typeof(string))).Returns("TEST");
            A.CallTo(() => serviceProvider.GetService(typeof(double))).Returns((double)1);

            var exportProvider = A.Fake <IExportProvider>();

            A.CallTo(() => exportProvider.GetExport(typeof(int), null)).Returns(2);
            A.CallTo(() => exportProvider.GetExport(typeof(double), TestImportName)).Returns((double)3);
            A.CallTo(() => exportProvider.GetExport(typeof(decimal), null)).Returns((decimal)4);

            var instance = ActionActivator.CreateInstance(actionInfo, serviceProvider, exportProvider);

            Assert.NotNull(instance);
            Assert.IsType <ActionWithImports>(instance);

            var instance2 = (ActionWithImports)instance;

            Assert.Equal("TEST", instance2.Service1);
            Assert.Equal(1, instance2.Service2);
            Assert.Equal(2, instance2.Import1);
            Assert.Equal(3, instance2.Import2);
            Assert.Equal(4, instance2.Import3);
        }
示例#2
0
        public void CreateActionInstanceWithMissingServiceTest()
        {
            var actionInfo     = ActionActivator.GetActionInfo(typeof(ActionWithOptionalService));
            var exportProvider = A.Fake <IExportProvider>();

            var serviceProvider = A.Fake <IServiceProvider>();

            A.CallTo(() => serviceProvider.GetService(typeof(string))).Returns(null);

            Assert.Throws <InvalidOperationException>(
                () => ActionActivator.CreateInstance(actionInfo, serviceProvider, exportProvider));
        }
示例#3
0
        public void CreateActionInstanceWithMissingImportTest()
        {
            var actionInfo      = ActionActivator.GetActionInfo(typeof(ActionWithImportsOfSameType));
            var serviceProvider = A.Fake <IServiceProvider>();

            var exportProvider = A.Fake <IExportProvider>();

            A.CallTo(() => exportProvider.GetExport(typeof(int), null)).Returns(null);

            Assert.Throws <InvalidOperationException>(
                () => ActionActivator.CreateInstance(actionInfo, serviceProvider, exportProvider));
        }
示例#4
0
        public void CreateActionInstanceWithOptionalServiceTest()
        {
            var actionInfo     = ActionActivator.GetActionInfo(typeof(ActionWithOptionalService));
            var exportProvider = A.Fake <IExportProvider>();

            var serviceProvider = A.Fake <IServiceProvider>();

            A.CallTo(() => serviceProvider.GetService(typeof(string))).Returns("TEST");
            A.CallTo(() => serviceProvider.GetService(typeof(int))).Returns(null);

            var instance = ActionActivator.CreateInstance(actionInfo, serviceProvider, exportProvider);

            Assert.NotNull(instance);
            Assert.IsType <ActionWithOptionalService>(instance);

            var instance2 = (ActionWithOptionalService)instance;

            Assert.Equal("TEST", instance2.Service1);
            Assert.Equal(-1, instance2.Service2);
        }
示例#5
0
        public void CreateActionInstanceWithImportsOfSameTypeTest()
        {
            var actionInfo      = ActionActivator.GetActionInfo(typeof(ActionWithImportsOfSameType));
            var serviceProvider = A.Fake <IServiceProvider>();

            var exportProvider = A.Fake <IExportProvider>();

            A.CallTo(() => exportProvider.GetExport(typeof(int), null)).Returns(1);
            A.CallTo(() => exportProvider.GetExport(typeof(int), TestImportName)).Returns(2);
            A.CallTo(() => exportProvider.GetExport(typeof(int), TestImportName2)).Returns(3);

            var instance = ActionActivator.CreateInstance(actionInfo, serviceProvider, exportProvider);

            Assert.NotNull(instance);
            Assert.IsType <ActionWithImportsOfSameType>(instance);

            var instance2 = (ActionWithImportsOfSameType)instance;

            Assert.Equal(1, instance2.Import1);
            Assert.Equal(2, instance2.Import2);
            Assert.Equal(3, instance2.Import3);
        }