0

I'm trying to setup an xunit test such that when It tries to access a instance variable on the returned ExampleConfig object in var config it doesn't throw an exception. Current when debugging the test the value of config is null.

var config = configuration.GetSection(configSection)
     .Get<ExampleConfig>();

GetSection returns an IConfigurationSection and the chained Get returns ExampleConfig.

What I've tried

        [Fact]
        public Task GivenConfiguration_WhenMyMethodCalled_ThenReturnServices()
        {
            //Arrange
            var configSection = $"{Assembly.GetExecutingAssembly().GetName().Name}:{nameof(ExampleConfig)}";

            var servicesMock = Substitute.For<IServiceCollection>();
            var configurationMock = Substitute.For<IConfiguration>();
            var configurationSectionMock = Substitute.For<IConfigurationSection>();

            var exampleConfig = new ExampleConfig()
            {
                Url = "123456"
            };

            configurationSectionMock.Value.Returns(exampleConfig.ToString());

            configurationMock.GetSection(configSection).Returns(configurationSectionMock);

            //Act
            var result = servicesMock.MyMethod(configurationMock);

            //Assert
            Assert.IsAssignableFrom<IServiceCollection>(result);
            return Task.CompletedTask;
        }
James Moore
  • 228
  • 3
  • 9

0 Answers0