【发布时间】:2010-09-09 07:27:47
【问题描述】:
这篇文章展示了如何测试设置 cookie,然后在 ViewData 中查看它。我要做的是查看是否写入了正确的 cookie(值和名称)。任何回复、博客文章或文章将不胜感激。
【问题讨论】:
这篇文章展示了如何测试设置 cookie,然后在 ViewData 中查看它。我要做的是查看是否写入了正确的 cookie(值和名称)。任何回复、博客文章或文章将不胜感激。
【问题讨论】:
您是否正在寻找类似的东西? (未经测试,只是在回复框中输入)
var cookies = new HttpCookieCollection();
controller.ControllerContext = new FakeControllerContext(controller, cookies);
var result = controller.TestCookie() as ViewResult;
Assert.AreEqual("somevaluethatshouldbethere", cookies["somecookieitem"].Value);
正如你的意思是你想测试一个cookie的写入而不是读取一个? 如果可能,请让您的要求更清楚:)
【讨论】:
也许您需要传入一个用于写入 cookie 的 Fake Response 对象,然后测试从控制器返回的内容。
【讨论】:
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));
}
【讨论】: