这篇文章展示了如何测试设置 cookie,然后在 ViewData 中查看它。我要做的是查看是否写入了正确的 cookie(值和名称)。任何回复、博客文章或文章将不胜感激。
jdelator
问问题
4888 次
3 回答
7
Are you looking for something more like this? (untested, just typed it up in the reply box)
var cookies = new HttpCookieCollection();
controller.ControllerContext = new FakeControllerContext(controller, cookies);
var result = controller.TestCookie() as ViewResult;
Assert.AreEqual("somevaluethatshouldbethere", cookies["somecookieitem"].Value);
As in, did you mean you want to test the writing of a cookie instead of reading one? Please make your request clearer if possible :)
于 2008-09-16T05:17:05.237 回答
1
也许您需要传入一个写入 cookie 的 Fake Response 对象,然后测试从 Controller 中返回的内容。
于 2008-09-16T05:23:37.870 回答
-2
function ReadCookie(cookieName) {
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName);
if (ind==-1 || cookieName=="") return "";
var ind1=theCookie.indexOf(';',ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
于 2008-09-16T05:09:10.963 回答