// Only use [ClassInitialize] when the class properties // should only be initalized once for all tests. // If the test methods change the properties, // use [TestInitialize] or add a public constructor. // If you're not sure, just use the unit test's // public constructor. public ServiceCollectionForBusinessTest() { var configurationBuilder = new ConfigurationBuilder() .AddJsonFile("appsettings.json"); var configuration = configurationBuilder.Build(); var serviceCollection = new ServiceCollection(); var serviceCollectionForBusiness = new ServiceCollectionForBusiness(configuration, configurationBuilder); serviceCollectionForBusiness.RegisterDependencies(serviceCollection); _serviceProvider = serviceCollection.BuildServiceProvider(); }
// Only use [ClassInitialize] when the class properties // should only be initalized once for all tests. // If the test methods change the properties, // use [TestInitialize] or add a public constructor. // If you're not sure, just use the unit test's // public constructor. public ServiceCollectionForBusinessTest() { // The mock configuration must setup all of the // expected properties or an exception is thrown. // System.ArgumentNullException: Value cannot be null. // Value cannot be null.\r\nParameter name: configuration var mockConfigurationSection = new Mock <IConfigurationSection>(); mockConfigurationSection.SetupGet(x => x[It.IsAny <string>()]).Returns("expected configuration value"); var mockConfiguration = new Mock <IConfiguration>(); mockConfiguration.Setup(x => x.GetSection(It.IsAny <string>())).Returns(mockConfigurationSection.Object); var serviceCollection = new ServiceCollection(); var serviceCollectionForBusiness = new ServiceCollectionForBusiness(); serviceCollectionForBusiness.RegisterDependencies(mockConfiguration.Object, serviceCollection); _serviceProvider = serviceCollection.BuildServiceProvider(); }