Esempio n. 1
0
        public async Task TryCreateAsync_WithContext_ReturnsNewBinding()
        {
            var bindingValue = "a string";
            var serviceProviderHolderMock = new Mock <IServiceProviderHolder>();
            var serviceProviderHolder     = serviceProviderHolderMock.Object;

            serviceProviderHolderMock.Setup(mock => mock.GetRequiredService(typeof(string)))
            .Returns(bindingValue);
            var injectBindingProvider = new InjectBindingProvider(serviceProviderHolder);
            var type      = typeof(ParameterInfoProvider);
            var method    = type.GetMethod("WithParameter");
            var parameter = method.GetParameters().Single(m => m.Name == "x");

            var context = new BindingProviderContext(parameter, new Dictionary <string, Type>(), CancellationToken.None);

            var binding = await injectBindingProvider.TryCreateAsync(context);

            binding.Should().NotBeNull().And.BeAssignableTo <IBinding>();
        }
Esempio n. 2
0
        public async Task InjectBindingProviderDoesSomething()
        {
            try
            {
                var mockService         = new Mock <ITestService>();
                var mockServiceProvider = new Mock <IServiceProvider>();
                mockServiceProvider
                .Setup(p => p.GetService(typeof(ITestService)))
                .Returns(mockService.Object);

                //mockServiceProvider
                //    .Setup(p => p.GetService(typeof(IFunctionFilter)))
                //    .Returns<IFunctionFilter>(x =>
                //    {
                //        return new ScopeCleanupFilter();
                //    });

                var mockServiceScope = new Mock <IServiceScope>();
                mockServiceScope
                .Setup(s => s.ServiceProvider)
                .Returns(mockServiceProvider.Object);

                var mockServiceScopeFactory = new Mock <IServiceScopeFactory>();
                mockServiceScopeFactory
                .Setup(f => f.CreateScope())
                .Returns(mockServiceScope.Object);

                mockServiceProvider
                .Setup(p => p.GetService(typeof(IServiceScopeFactory)))
                .Returns(mockServiceScopeFactory.Object);

                var method     = typeof(HttpTestFunction).GetMethod("Run");
                var parameters = method.GetParameters();

                var parameterInfo = parameters.First(x => x.CustomAttributes.Count() > 0 && x.CustomAttributes.Any(c => c.AttributeType == typeof(InjectAttribute)));

                var provider          = new InjectBindingProvider(mockServiceProvider.Object);
                var cancellationToken = new CancellationToken();

                var context = new BindingProviderContext(
                    parameterInfo,
                    null,
                    cancellationToken);

                var binding = await provider.TryCreateAsync(context);

                Assert.IsNotNull(binding);

                var bindingContext = new BindingContext(
                    new ValueBindingContext(
                        new FunctionBindingContext(Guid.NewGuid(), cancellationToken),
                        cancellationToken),
                    null);
                var valueProvider = await binding.BindAsync(bindingContext);

                Assert.IsNotNull(valueProvider);

                var result = await valueProvider.GetValueAsync();

                Assert.IsNotNull(result);
                Assert.IsInstanceOf <ITestService>(result);
            }
            catch (Exception e)
            {
                TestContext.WriteLine(e);
                throw;
            }
        }