コード例 #1
0
        public void Dependency_injection_smtpOtions3()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddOptions();
            var viewEngine       = new Mock <IRazorViewEngine>();
            var tempDataProvider = new Mock <ITempDataProvider>();
            var logger           = new Mock <ILogger <EmailService> >();

            serviceCollection.AddSingleton(logger.Object);
            serviceCollection.AddSingleton(viewEngine.Object);
            serviceCollection.AddSingleton(tempDataProvider.Object);

            serviceCollection.Configure <DefaultEmailServiceOptions>(o =>
            {
                o.PrepareSmtpClientAsync = () => throw new FactExcetpionForSmtpCreation();
            });

            serviceCollection.AddPostal();

            var services     = serviceCollection.BuildServiceProvider();
            var emailService = services.GetRequiredService <IEmailService>();

            Assert.ThrowsAsync <FactExcetpionForSmtpCreation>(() => emailService.SendAsync(new Email("testView")));
            DefaultEmailServiceOptions emailOptionField = GetInstanceField(typeof(EmailService), emailService, "options") as DefaultEmailServiceOptions;

            Assert.Throws <FactExcetpionForSmtpCreation>(() => emailOptionField.PrepareSmtpClientAsync().Result);
        }
コード例 #2
0
        public void Dependency_injection_smtpOtions2()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddOptions();
            var viewEngine       = new Mock <IRazorViewEngine>();
            var tempDataProvider = new Mock <ITempDataProvider>();
            var logger           = new Mock <ILogger <EmailService> >();

            serviceCollection.AddSingleton(logger.Object);
            serviceCollection.AddSingleton(viewEngine.Object);
            serviceCollection.AddSingleton(tempDataProvider.Object);

            serviceCollection.Configure <DefaultEmailServiceOptions>(o =>
            {
                o.Host     = "abc";
                o.Port     = 12345;
                o.UserName = "******";
                o.Password = "******";
                o.PrepareSmtpClientAsync = () => Task.FromResult <SmtpClient>(new FactExcetpionForSmtpClient());
            });

            serviceCollection.AddPostal();

            var services     = serviceCollection.BuildServiceProvider();
            var emailService = services.GetRequiredService <IEmailService>();

            DefaultEmailServiceOptions emailOptionField = GetInstanceField(typeof(EmailService), emailService, "options") as DefaultEmailServiceOptions;

            emailOptionField.Host.ShouldBe("abc");
            emailOptionField.Port.ShouldBe(12345);
            emailOptionField.UserName.ShouldBe("zxcvbn");
            emailOptionField.Password.ShouldBe("asdfgh");

            emailOptionField.PrepareSmtpClientAsync().Result.ShouldBeOfType <FactExcetpionForSmtpClient>();
        }