使用 ASP.NET Core 建立 Minimal Web API 並進行 Web Integration Test (xUnit)

  • 1590
  • 0
  • 2022-07-05

為了配合整體教育訓練的課程規劃,雖說 Web API 不是自己最專業領域,但仍是需帶點撰寫 Web API 的教學,才能讓學員們對 App 如何去介接後端資料有所了解,因此就開啟了一段 ASP.NET Core 6.0 的 Minimal Web API 教學。

畢竟 ASP.NET Core 6.0 的 Minimal Web API 使用了極簡的 Code,很適合給初心者在認識之初,不用了解太多特殊的設定就能快速上手,打造出一套含有 CRUD 功能的 Web API。

但若要完整透徹的了解 ASP.NET Core 6.0+ 所設計的 Minimal Web API 的整體概念(可參考文末的參考文件),仍是需要花上很多心力去摸索其基礎觀念與設計的,請務必釐清。 

 

在教學時也希望能夠帶給初心者的學員有正確開發觀念,所以過程中除了撰寫 Web API 外,也同時讓學員去撰寫 Integration Test 來測試自己產出的 Web API,在這邊則是選擇使用了 xUnit 來做為 Web API 的 Integration Test 所使用的 Framework。

 

然後卻在一開始寫完第一個 Web API 的 Integration Test 就撞牆了…🥲


一開始用 Visual Studio 建立一個 ASP.NET Core 的 Web API 的專案類型並且將 "使用控制器" 與 "啟用 OpenAPI 支援" 的勾選 取消 後,以建立 Minimal Web API 專案。

 

經過撰寫 DataContext 與 Model 的部分後,大致建立出一個可透過 Http 的 Get 操作的 WebAPI。

若透過 Visual Studio 直接執行專案,啟動瀏覽器的時候會需要註冊 SSL 憑證到電腦當中。

然後就能在瀏覽器當中看到正常執行 Root WebApi 的回應。

 

所以再來就是進行該 Web API 的 Integration Test 撰寫,在該方案底下新建一個 xUnit 的測試專案,並且設定 Reference 至 Web API 專案。

 

緊接著,就是開始著手撰寫第一個測試案例。

 

來測試是否能夠進行 Web API 的 Integration Test,興高采烈地按下 Visual Studio 內建進行測試的 "Ctrl + R, A" 快捷鍵,卻在測試總管中得到了下列的錯誤訊息。

System.InvalidOperationException : Can't find '<Your Project Path>\bin\Debug\net6.0\testhost.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the testhost.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.

 

後來找了一下此問題的處理,主要的原因是 ASP.NET Core 6 引入了全新設計的 WebApplication 類別,並且在相關的 Intergration Test 文件上指出,若不使用 Startup 類別而透過 WebApplicationFactory 來進行其 Intergration Test 的測試,請:

 "An ASP.NET Core 6 app needs to expose the implicitly defined Program class to the test project…"

有需深入研究的捧友,可看文件的 basic-tests-with-the-default-webapplicationfactory 段落來了解:
https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory

本文此處採用文件上的解法二。

在原本 Web API 的專案打開 Program.cs 後,並到原本撰寫的 Web API 程式的最後再加上 public partial class Program { }

 

再次在 Visual Stduio 按下測試快捷鍵 "Ctrl + R, A",測試總管顯示綠燈✅✅✅。

 

參考文件:

  1. ASP.NET Core 極簡風 - Minimal API:
    https://blog.darkthread.net/blog/minimal-api/
  2. Tutorial: Create a minimal web API with ASP.NET Core:
    https://docs.microsoft.com/en-us/aspnet/core/tutorials/min-web-api

 


 


I'm a Microsoft MVP - Developer Technologies (From 2015 ~).
 

MVP_Logo



I focus on the following topics: Xamarin Technology, Azure, Mobile DevOps, and Microsoft EM+S.

If you want to know more about them, welcome to my website:
https://jamestsai.tw 


本部落格文章之圖片相關後製處理皆透過 Techsmith 公司 所贊助其授權使用之 "Snagit" 與 "Snagit Editor" 軟體製作。