views:

1400

answers:

5

http://weblogs.asp.net/stephenwalther/archive/2008/06/30/asp-net-mvc-tip-12-faking-the-controller-context.aspx

This post shows how to test setting a cookie and then seeing it in ViewData. What I what to do is see if the correct cookies were written (values and name). Any reply, blog post or article will be greatly appreciated.

A: 

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)); }

dimarzionist
+5  A: 

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 :)

EDIT: why in the world do i get a -1 for asking a question and the person who pasted client-side javascript get a +1?? (incase people dont realise you can't use client-side JS as there isn't a browser ... )

White Dragon
Those cookies belong to the request object and not the response object. I had already tried this.
jdelator
+1  A: 

Perhaps you need to pass in a Fake Response object that the cookies are written to, and you test what is returned in that from the Controller.

porum
A: 

polite bump

jdelator
+1  A: 

Check out my post in this other thread.

Graphain