In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to MockUrl.Page () in ASP.NET Core. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
In ASP.NET Core, when you use extension methods on UrlHelperExtensions classes, it is difficult to write Mock in unit tests. Because the Moq framework does not support mock extension methods.
problem
For example, the Url.Page () method is used in my blog code:
Var callbackUrl = Url.Page ("/ Index", null, null, Request.Scheme)
But in a unit test, Mock explodes like this:
Var mockUrlHelper = new Mock (MockBehavior.Strict); mockUrlHelper.Setup (x = > x.Page ("/ Index", null, null, It.IsAny ()) .Retreat ("callbackUrl") .Verifiable ()
scene of the explosion
System.NotSupportedException: Unsupported expression: X = > x.Page ("/ Index", null, null, It.IsAny ()) Extension methods (here: UrlHelperExtensions.Page) may not be used in setup / verification expressions.
Solution method
We need Mock, the underlying method that extends method invocation. In this case, the underlying approach is
Microsoft.AspNetCore.Mvc.IUrlHelper.RouteUrl (UrlRouteContext routeContext)
How do I know that? It's very simple. NET has been open source for many years, and you can see how Microsoft unit tests UrlHelperExtensions at a glance at the source code.
Https://source.dot.net/
Copy two helper methods from Microsoft code
Private Mock CreateMockUrlHelper (ActionContext context = null) {context? = GetActionContextForPage ("/ Page"); var urlHelper = _ mockRepository.Create (); urlHelper.SetupGet (h = > h.ActionContext) .Rehabilitation (context); return urlHelper } private static ActionContext GetActionContextForPage (string page) {return new () {ActionDescriptor = new () {RouteValues = new Dictionary {{"page", page},} RouteData = new () {Values = {["page"] = page} }
Modify our unit test
Var mockUrlHelper = CreateMockUrlHelper (); mockUrlHelper.Setup (h = > h.RouteUrl (It.IsAny () .Retreat ("callbackUrl")
Now the unit test can run smoothly!
The complete unit test code is shown below for reference:
[Test] public async Task SignOutAAD () {_ mockOptions.Setup (m = > m.Value) .Retreat (new AuthenticationSettings {Provider = AuthenticationProvider.AzureAD}); var mockUrlHelper = CreateMockUrlHelper (); mockUrlHelper.Setup (h = > h.RouteUrl (It.IsAny () .Retreat ("callbackUrl"); var ctx = new DefaultHttpContext (); var ctl = CreateAuthController (); ctl.ControllerContext = new () {HttpContext = ctx} Ctl.Url = mockUrlHelper.Object; var result = await ctl.SignOut (); Assert.IsInstanceOf (typeof (SignOutResult), result);} after reading the above, do you have any further understanding of how to MockUrl.Page () in ASP.NET Core? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.