Omu*_*Omu 4 asp.net-mvc unit-testing moq
谁知道如何嘲笑Url.Content("~")?
(顺便说一句:我正在使用Moq)
Dav*_*d M 10
你指的Url是控制器中的属性,我认为是属于类型的UrlHelper.我们能够模拟这个的唯一方法是提取一个IUrlHelper接口,并创建一个UrlHelperWrapper既实现它又包装本机UrlHelper类型的类.然后我们在我们的上面定义一个新属性BaseController:
public new IUrlHelper Url
{
get { return _urlHelper; }
set { _urlHelper = value; }
}
Run Code Online (Sandbox Code Playgroud)
这允许我们创建模拟IUrlHelper并注入它们,但在默认情况下使用我们UrlHelperWrapper类的实例.对不起,它啰嗦了,但正如你所发现的那样,这是一个问题.但是,它确实无需更改Url.Action控制器中的任何类似呼叫.
这是界面:
public interface IUrlHelper
{
string Action(string actionName);
string Action(string actionName, object routeValues);
string Action(string actionName, string controllerName);
string Action(string actionName, RouteValueDictionary routeValues);
string Action(string actionName, string controllerName, object routeValues);
string Action(string actionName, string controllerName, RouteValueDictionary routeValues);
string Action(string actionName, string controllerName, object routeValues, string protocol);
string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol, string hostName);
string Content(string contentPath);
string Encode(string url);
string RouteUrl(object routeValues);
string RouteUrl(string routeName);
string RouteUrl(RouteValueDictionary routeValues);
string RouteUrl(string routeName, object routeValues);
string RouteUrl(string routeName, RouteValueDictionary routeValues);
string RouteUrl(string routeName, object routeValues, string protocol);
string RouteUrl(string routeName, RouteValueDictionary routeValues, string protocol, string hostName);
}
Run Code Online (Sandbox Code Playgroud)
我不打算给你定义UrlHelperWrapper- 它实际上只是一个愚蠢的包装器来实现它,并将所有调用传递给UrlHelper.
| 归档时间: |
|
| 查看次数: |
3595 次 |
| 最近记录: |