想要实现一个测试场景,其中Integration Test调用DLL方法,该方法调用(通过REST)ASP.NET WebAPI应用程序(所有这些都在同一解决方案上)。当我启动测试(Ctrl+R,A
)时,ASP.NET WebAPI项目没有启动,DLL无法调用相应的端点。
问题是:如何在启动UnitTest时启动ASP.NET WebAPI项目?
我知道我可以制作一些其他测试场景,比如UnitTest直接调用WebAPI,但这是一个集成版本,并且想测试整个工作流程。
集成测试项目
[TestMethod]
public async Task MainWorkflow()
{
// Target is an object defined on the DLL
// and instantiated on the unit test [ClassInitialize]
string fileId = await Target.UploadFiles(theFile);
}
DLL项目
public async Task<String> UploadFiles(string fileToUpload)
{
var client = new RestClient("http://localhost:3000");
var request = new RestRequest(EndPoints.Upload, Method.PUT);
// Here a RestSharp code will communicate with localhost
IRestResponse response = await client.ExecuteTaskAsync(request);
return JsonConvert.DeserializeObject<string>(response.Content);
}
ASP.NET WebAPI
public class IntegrationController : ApiController
{
[HttpPut]
[Route("api/uploadFile")]
public async Task<string> UploadFiles()
{
HttpRequest req = HttpContext.Current.Request;
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), folderName, req.Files[0].FileName);
drawingFile.SaveAs(fileSavePath);
// some variables omitted for simplicity...
return theFileId;
}
}