如何使用 URLHelp 模拟静态类中的静态方法? (起订量)
我有一个扩展方法。任何人都可以帮助我如何用最小起订量测试这种方法吗?
public static string GetBaseUrl(this UrlHelper urlHelper)
{
Uri contextUri = new Uri(urlHelper.RequestContext.HttpContext.Request.Url, urlHelper.RequestContext.HttpContext.Request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = urlHelper.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
string url = realmUri.Uri.AbsoluteUri;
if (url.EndsWith("/"))
{
url = url.Remove(url.Length - 1, 1);
}
return url;
}
非常感谢。
I have a extension method. Can any one help me how to test this method with Moq?
public static string GetBaseUrl(this UrlHelper urlHelper)
{
Uri contextUri = new Uri(urlHelper.RequestContext.HttpContext.Request.Url, urlHelper.RequestContext.HttpContext.Request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = urlHelper.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
string url = realmUri.Uri.AbsoluteUri;
if (url.EndsWith("/"))
{
url = url.Remove(url.Length - 1, 1);
}
return url;
}
many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 TrueWill 指出的,您不能直接将 Moq 与 UrlHelper.RequestContext 一起使用,因为它不是虚拟的。另一方面,UrlHelper 是一个公共类,您可以实例化它以用于单元测试。
然而,在某些时候,您会遇到需要分配 HttpContextBase 来创建 UrlHelper,Moq 可以帮助您做到这一点。
这是一个测试,表明我至少可以编写一个调用 GetBaseUrl 而不抛出任何异常的单元测试:
但是,这不是编写和维护的最简单的单元测试,因此我支持 TrueWill 的评论,您可能会让生活变得更简单如果您将 UrlHelper 隐藏在界面后面,您自己。
As TrueWill points out, you can't use Moq directly with UrlHelper.RequestContext because it isn't virtual. On the other hand, UrlHelper is a public class that you can instantiate for use with unit testing.
At some point, however, you will encounter the need to assign a HttpContextBase to create the UrlHelper, and Moq can help you to do that.
Here's a test that shows that I can at least write a unit test that invokes your GetBaseUrl without throwing any exceptions:
However, this isn't the simplest unit test to write and maintain, so I support TrueWill's comment that you might make life simpler for yourself if you hide UrlHelper behind an interface.
UrlHelper.RequestContext 属性是非虚拟的。据我所知,起订量在这种情况下不会有帮助。
您可以为 UrlHelper 创建一个实现接口的包装类,但这似乎违背了使用扩展方法的目的。
如果您有商业程序的预算,Typemock 可能会满足您的需求。 (我还没有尝试过;我自己使用 Moq。)
另一种选择是针对此方法编写集成测试;虽然它们比单元测试运行得更慢,但我怀疑这种方法不太可能经常进行版本控制。
一个更大的问题是与 UrlHelper 的耦合降低了应用程序其余部分的可测试性。也许其他发帖者可以提出该问题的答案。
The UrlHelper.RequestContext property is non-virtual. Moq isn't going to be of help in this case, to the best of my knowledge.
You could create a wrapper class for UrlHelper that implements an interface, but that would seem to defeat the purpose of using an extension method.
Typemock would probably do what you want, if you have the budget for a commercial program. (I haven't tried it; I use Moq myself.)
Another option would be to write integration tests against this method; while they would run more slowly than unit tests, I suspect this method is unlikely to version often.
A larger issue is coupling to UrlHelper reducing testability in the rest of your application. Perhaps other posters can suggest answers to that issue.