从非web应用程序使用Asp.NETMVC路由(测试)
我正在使用Watin为使用T4MVC的Asp.NETMVC应用程序编写specflow测试 我发现自己在测试中使用了“魔术字符串”URL,这是我不喜欢的从非web应用程序使用Asp.NETMVC路由(测试),asp.net,asp.net-mvc,bdd,watin,specflow,Asp.net,Asp.net Mvc,Bdd,Watin,Specflow,我正在使用Watin为使用T4MVC的Asp.NETMVC应用程序编写specflow测试 我发现自己在测试中使用了“魔术字符串”URL,这是我不喜欢的 [Given(@"I am on the sign up page")] public void GivenIAmOnTheSignUpPage() { string rootUrl = ConfigurationManager.AppSettings["RootUrl"]; string fullUrl = string.Fo
[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
string rootUrl = ConfigurationManager.AppSettings["RootUrl"];
string fullUrl = string.Format("{0}/Authentication/Signup",rootUrl);
WebBrowser.Current.GoTo(fullUrl);
}
我更愿意像在MVC应用程序中一样使用我的T4MVC动作结果,类似这样的
[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
WebBrowser.Current.GoTo(MVC.Authentication.SignUp().ToAbsoluteUrl());
}
MyToAbsoluteUrl
扩展方法
public static class RouteHelper
{
private static UrlHelper _urlHelper;
private static string _rootUrl;
public static string ToAbsoluteUrl(this ActionResult result)
{
EnsureUrlHelperInitialized();
var relativeUrl = _urlHelper.Action(result);
return string.Format("{0}/{1}", _rootUrl, relativeUrl);
}
private static void EnsureUrlHelperInitialized()
{
if (_urlHelper==null)
{
_rootUrl = ConfigurationManager.AppSettings["RootUrl"];
var request = new HttpRequest("/", _rootUrl, "");
var response = new HttpResponse(new StringWriter());
var context = new HttpContext(request,response);
HttpContext.Current = context;
var httpContextBase = new HttpContextWrapper(context);
RouteTable.Routes.Clear();
MvcApplication.RegisterRoutes(RouteTable.Routes);
var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));
_urlHelper = new UrlHelper(requestContext, RouteTable.Routes);
}
}
}
初始化RequestContext和RouteCollection以便生成测试URL的正确方法是什么
目前我在var requestContext=newrequestcontext(httpContextBase,RouteTable.Routes.GetRouteData(httpContextBase))行上收到一个NullReferenceException代码>。这是创建requestContext的正确方法吗
或者,如果有更好的方法来获取ActionResult(来自T4MVC)并将其解析为web应用程序之外的绝对url,那才是我真正想要的
public static class RouteHelper
{
private static UrlHelper _urlHelper;
private static string _rootUrl;
static RouteHelper()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
var req = new HttpRequest(string.Empty, "http://www.site.com", null);
var res = new HttpResponse(null);
var ctx = new HttpContext(req, res); // do not use HttpContext.Current
var requestContext = new RequestContext(new HttpContextWrapper(ctx),
new RouteData());
_urlHelper = new UrlHelper(requestContext, routes);
_rootUrl = ConfigurationManager.AppSettings["RootUrl"];
}
public static string ToAbsoluteUrl(this ActionResult result)
{
return string.Format("{0}{1}", _rootUrl, _urlHelper.Action(result));
}
}
静态构造函数设置您的私有字段。我选择使用一个新的RouteCollection,而不是使用静态RouteTable.Routes属性,但您可能能够
我认为HttpRequest和HttpResponse的构造函数并不重要。我只是传入了一些字符串,让它们在不引发异常的情况下进行构造。使用这些来构建一个全新的HttpContext(从xUnit运行时不要使用HttpContext.Current)。然后,您可以将其放入HttpContextWrapper以获取HttpContextBase引用
构造一个新的RequestContext,传入基本包装器和一个新的RoutedData实例。将其与上一个RouteCollection一起用于构造UrlHelper。请注意,它的Action方法将返回以“/”开头的字符串,因此您应该将其保留在RootUrl应用设置之外(因此使用类似value=”的内容)https://develop.site.com“不带尾随斜杠)
注意:这不适用于MVC区域中定义的管线。为此,除了在全局asax中调用RegisterRoutes外,还需要注册这些区域 您在SpecFlow中使用什么单元测试库?MsTest、NUnit或其他什么?我正在使用xUnit,这有关系吗?不,我想知道如何定制我的答案。我不确定[TestClass]和[AssemblyInitialize]的xunit等价物是什么,但想象一下不难找到。您可以发布NullReferenceException的堆栈跟踪吗?什么类/方法实际抛出了它?T4MVC提供采用ActionResult参数的UrlHelper.Action重载。不过,这些重载在后台使用UrlHelper的RouteUrl,所以要让它按您想要的方式工作,您必须手动设置RequestContext、HttpContextBase和HttpRequestBase对象。谢谢。我确实将它改为使用“sure”方法,而不是类型初始值设定项,因为当出现问题时,这些方法往往会抛出非常奇怪的异常,并且考虑到这是从配置读取的,这很可能发生。