Skip to content

Instantly share code, notes, and snippets.

@keithbloom
Created October 28, 2011 20:54
NullReferenceRhinoMocks
public void SetCookie(IHttpContextService context)
{
if (context == null)
return;
if (context.User == null)
return;
if (context.User.Identity == null)
return;
if (!context.User.Identity.IsAuthenticated)
return;
this.UserName = context.User.Identity.Name;
// Create a cookie for username and key
var cookie = new HttpCookie("Token", this.UserNameOnly + "|" + GetKey(this.Input));
context.CookieCollection.Add(cookie);
}
[Test]
public void Should_SetACookie()
{
var httpContextService = MockRepository.GenerateStub<IHttpContextService>();
var cookieCollection = new HttpCookieCollection();
IPrincipal user = new GenericPrincipal(new GenericIdentity("Test"),
new[] {"TestRole"});
httpContextService.Stub(c => c.User).Return(user);
httpContextService.Stub(c => c.CookieCollection).Return(cookieCollection);
cookieSetter.SetCookie(httpContextService);
httpContextService.VerifyAllExpectations();
}
httpContextService.Stub(c => c.User).Return(user).Repeat.Any();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment