Esempio n. 1
0
        public static void BindConfiguration_ReturnsSameBuilderInstance()
        {
            var services       = new ServiceCollection();
            var optionsBuilder = new OptionsBuilder <FakeOptions>(services, Options.DefaultName);

            var returnedBuilder = optionsBuilder.BindConfiguration("Test");

            Assert.Same(optionsBuilder, returnedBuilder);
        }
Esempio n. 2
0
        public static void BindConfiguration_ThrowsForNullBuilder()
        {
            OptionsBuilder <FakeOptions> optionsBuilder = null !;

            Assert.Throws <ArgumentNullException>("optionsBuilder", () =>
            {
                optionsBuilder.BindConfiguration("test");
            });
        }
Esempio n. 3
0
        public static void BindConfiguration_ThrowsForNullConfigurationSectionPath()
        {
            var    services          = new ServiceCollection();
            var    optionsBuilder    = new OptionsBuilder <FakeOptions>(services, Options.DefaultName);
            string configSectionPath = null !;

            Assert.Throws <ArgumentNullException>("configSectionPath", () =>
            {
                optionsBuilder.BindConfiguration(configSectionPath);
            });
        }
        public static void BindConfiguration_OptionsMaterializationThrowsIfNoConfigurationInDI()
        {
            var services = new ServiceCollection();
            OptionsBuilder <FakeOptions> optionsBuilder = services.AddOptions <FakeOptions>();

            _ = optionsBuilder.BindConfiguration("Test");
            using ServiceProvider serviceProvider = services.BuildServiceProvider();

            Assert.ThrowsAny <InvalidOperationException>(() =>
            {
                _ = serviceProvider.GetRequiredService <IOptions <FakeOptions> >();
            });
        }
        public static void BindConfiguration_UpdatesOptionOnConfigurationUpdate()
        {
            const string configSectionName = "Test";
            string       messageConfigKey  = ConfigurationPath.Combine(configSectionName, nameof(FakeOptions.Message));
            const string messageValue1     = "This is a test";
            const string messageValue2     = "This is the message after update";

            FakeConfigurationSource configSource = new()
            {
                InitialData = new Dictionary <string, string?>
                {
                    [messageConfigKey] = messageValue1
                }
            };

            var services = new ServiceCollection();

            services.AddSingleton <IConfiguration>(new ConfigurationBuilder()
                                                   .Add(configSource)
                                                   .Build());
            OptionsBuilder <FakeOptions> optionsBuilder = services.AddOptions <FakeOptions>();

            _ = optionsBuilder.BindConfiguration(configSectionName);
            using ServiceProvider serviceProvider = services.BuildServiceProvider();
            var  optionsMonitor = serviceProvider.GetRequiredService <IOptionsMonitor <FakeOptions> >();
            bool updateHasRun   = false;

            optionsMonitor.OnChange((opts, name) =>
            {
                updateHasRun = true;
            });
            FakeOptions optionsValue1 = optionsMonitor.CurrentValue;

            Assert.Equal(messageValue1, optionsValue1.Message);
            configSource.Provider.Set(messageConfigKey, messageValue2);
            FakeOptions optionsValue2 = optionsMonitor.CurrentValue;

            Assert.True(updateHasRun);
            Assert.Equal(messageValue2, optionsValue2.Message);
        }
    }
        public static void BindConfiguration_UsesConfigurationRootIfSectionNameIsEmptyString()
        {
            const string messageValue  = "This is a test";
            var          configEntries = new Dictionary <string, string?>
            {
                [nameof(FakeOptions.Message)] = messageValue
            };
            var services = new ServiceCollection();

            services.AddSingleton <IConfiguration>(new ConfigurationBuilder()
                                                   .AddInMemoryCollection(configEntries)
                                                   .Build());
            OptionsBuilder <FakeOptions> optionsBuilder = services.AddOptions <FakeOptions>();

            _ = optionsBuilder.BindConfiguration(configSectionPath: "");

            using ServiceProvider serviceProvider = services.BuildServiceProvider();
            FakeOptions options = serviceProvider.GetRequiredService <IOptions <FakeOptions> >().Value;

            Assert.Equal(messageValue, options.Message);
        }