Web API 与 Azure Key Vault 的集成测试

Integration Tests for Web API with Azure Key Vault

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

所以当我运行实际项目时,Iconfiguration有7个属性。

但是我运行测试的时候,只有一个属性。

所以我在想,也许我在配置 AppSetting.json 文件的测试方法中遗漏了一些东西..

这是我的测试方法:

  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;
            }

        }

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

namespace CIS.API.Student
{
    public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }


        public IConfiguration Configuration { get; set; }
            services.AddSingleton(Configuration);
            services.AddMvc();
        }


        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(x => x
                 .AllowAnyOrigin()
                 .AllowAnyMethod()
                 .AllowAnyHeader()
                 .AllowCredentials());

            app.UseMvc();

        }
    }
}

有人知道为什么会这样吗?

经过一些研究,包括教程:Integration tests in ASP.NET Core,我成功了。

第 1 步:将 "appsetting.json" 文件复制到集成测试项目。

第 2 步:将测试 class 构造函数修改为:

 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;
            }

        }
}