Exemple #1
0
        public void GetConfigurationStrTest()
        {
            // Arrange 初始准备
            var logger            = new Mock <ILogger <WeatherForecastController> >();
            var mockConfiguration = new Mock <IConfiguration>();

            //mockConfiguration.SetupGet(_ => _.GetSection("WxConfig").Value).Returns("{\"AppId\": \"aaa\",\"AppSecret\": \"bbb\" }");
            mockConfiguration.SetupGet(_ => _.GetSection("WxConfig:AppId").Value).Returns("aaa");
            mockConfiguration.SetupGet(_ => _.GetSection("WxConfig:AppSecret").Value).Returns("bbb");
            var mockWxConfigOptions = new Mock <IOptions <WxConfigModel> >();

            mockWxConfigOptions.Setup(_ => _.Value).Returns(new WxConfigModel
            {
                AppId     = "aaa",
                AppSecret = "bbb"
            });
            var controller = new WeatherForecastController(logger.Object, mockConfiguration.Object, mockWxConfigOptions.Object);
            // Act 行为
            var result = controller.GetConfigurationStr("WxConfig:AppSecret");

            // Assert 断言

            Assert.AreEqual("bbb", result);
        }