使用Azure Key Vault进行Web API集成测试

时间:2018-04-23 23:05:00

标签: c# azure asp.net-core integration-testing azure-keyvault

我遵循了教程here,但似乎启动文件无法识别appsetting.json文件。

因此,当我运行实际项目时,dict有7个属性。 enter image description here

但是当我运行测试时,它只有一个属性。 enter image description here

所以我想也许我在测试方法中错过了配置AppSetting.json文件的内容..

这是我的测试方法:

Iconfiguration

这是我的启动类,显然我添加了json文件,其中包含Web API中所需的所有密钥和机密。

  public class StudentServiceRequestsTest
    {
        private readonly TestServer _server;
        private readonly HttpClient _client;

        public IndividualServiceRequestsTest()
        {
            // Arrange
            _server = new TestServer(new WebHostBuilder()
                .UseStartup<Startup>());
            _client = _server.CreateClient();
        }
        [Fact]
        public async Task GetStudentsByDeptShould()
        {
            try
            {
                //Act
                var response = await _client.GetAsync("/api/department/200/students");
                response.EnsureSuccessStatusCode();

                var responseString = await response.Content.ReadAsStringAsync();

                //Assert
                Assert.Equal("hi", responseString);
            }
            catch (Exception e)
            {
                throw;
            }

        }

任何人都知道为什么会这样?

1 个答案:

答案 0 :(得分:2)

经过一些研究,包括教程:Integration tests in ASP.NET Core,我让它发挥了作用。

第1步:复制&#34; appsetting.json&#34;归档到集成测试项目。

步骤2:将测试类构造函数修改为:

 public class StudentServiceTest 
    {
        private readonly TestServer _server;
        private readonly HttpClient _client;
        public StudentServiceTest()
        {
            var config = new ConfigurationBuilder().SetBasePath(Path.GetFullPath(@"..\..\..\..\Student.IntegrationTest"))
                                                   .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                                   .AddEnvironmentVariables();
            var builtConfig = config.Build();
            config.AddAzureKeyVault(
                    $"https://{builtConfig["Vault"]}.vault.azure.net/",
                    builtConfig["ClientId"],
                    builtConfig["ClientSecret"]);
            var Configuration = config.Build();

            _server = new TestServer(WebHost.CreateDefaultBuilder()
                .UseConfiguration(Configuration)
                .UseStartup<Startup>());
            _client = _server.CreateClient();
            _client.BaseAddress = new Uri("http://localhost:xxxxx");
        }

        [Fact]
        public async Task StudentShould()
        {
            try
            {
                //Act
                var response = await _client.GetAsync("/api/getStudentByID/200");
                response.EnsureSuccessStatusCode();

                var responseString = await response.Content.ReadAsStringAsync();

                //Assert
                Assert.Equal("bla bla", responseString);
            }
            catch (Exception e)
            {
                throw;
            }

        }
}