簡體   English   中英

Web API與Azure Key Vault的集成測試

[英]Integration Tests for Web API with Azure Key Vault

我在這里按照教程進行操作但似乎啟動文件無法識別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;
            }

        }

這是我的啟動類,適當地,我添加了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();

        }
    }
}

有人知道為什么會發生嗎?

經過一些研究,包括教程: ASP.NET Core中的集成測試 ,我使它起作用。

步驟1:將“ appsetting.json”文件復制到集成測試項目中。

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

        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM