Edi Wang

Azure Developer, Microsoft MVP


ASP.NET Core Unit Test: How to Mock HttpClient.GetStringAsync()

In ASP.NET Core unit tests, if you want to mock HttpClient.GetStringAsync(), here's the trick. Problem Given the following code var html = await _httpClient.GetStringAsync(sourceUrl); When I tried to mock HttpClient.GetStringAsync() like this var httpClientMock = new Mock<HttpClient>(); httpClientMock .Setup(p => p.GetStringAsync(It.IsAny<string>())) .Returns(Task.FromResult("...")); Moq …

ASP.NET Core Unit Test

ASP.NET Core Unit Test: How to Mock HttpContext.Features.Get()

In ASP.NET Core unit tests, if you want to mock HttpContext.Features.Get<SomeType>(), here's the trick. Problem I have my Error page code that will get exception detail infomation, to do that, I use HttpContext.Features.Get<IExceptionHandlerPathFeature>(). public void OnGet() { var requestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; var exceptionFeature = HttpContext.Features. …

ASP.NET Core Unit Test

ASP.NET Core Unit Test: How to Mock Url.Page()

Problem In ASP.NET Core, when you use extension methods on UrlHelperExtensions class, it would be difficult to write Mock in unit test. Because Moq doesn't support mocking extension methods. For example, the following code that I use in my blog is using Url.Page() method: var callbackUrl = Url.Page("/Index", null, null, Request.Scheme); But in my unit test, mocking like this will throw …

ASP.NET Core Unit Test

ASP.NET Core 5.0 Throughput Test in Kestrel, IIS, Nginx and Caddy

Starting from version 2.2, ASP.NET Core allow you to use the InProcess mode to improve performance under IIS. Rick Strahl has a detailed article on this. Three years have passed, and now ASP.NET Core has reached version 5.0, how the performance diffs between servers? Let's take a look together. Rick's Test Result In the original article, Rick Strahl tested the performance of ASP.NET Core 2.2 in …

IIS Linux ASP.NET Core

Configure Continuous Delivery for ASP.NET Core Website on Azure

Today I am trying new DevOps tools in VS2017 and Azure, my goal is to automate the process from development to production for an exsiting ASP.NET Core project. I have encountered some issues, and with the help of Microsoft Support, I was able to solve them and share with you guys. First, there are two ways to configure CD for Azure Web Apps. I prefer create a website first, then configure it from …

Azure .NET Core DevOps ASP.NET Core

  • 1