0

I want to write tests for the code below. For this I want to inject in a mocked HttpContext using RhinoMocks.

HttpContext is sealed though.

How should I handle this?

Thanks

    public PaxiumPrincipal CreatePrincipalFromCookie(HttpContext context)
    {
        HttpCookie authCookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];

        if (authCookie == null)
        {
            return null;
        };

        var authTicket = FormsAuthentication.Decrypt(authCookie.Value);

        if (authTicket == null)
        {
            return null;
        };

        var userPrincipal = new PaxiumPrincipal(new GenericIdentity(authTicket.Name), null);

        return userPrincipal;
    }
Dave Amour
  • 155
  • 1
  • 13
  • 4
    You can't. Write an adapter for HttpContext and mock it instead – Igor Popov Jun 01 '15 at 11:30
  • Here's a more general version of your [question](http://stackoverflow.com/questions/6484/how-do-you-mock-a-sealed-class) – juharr Jun 01 '15 at 11:35
  • Igor - by Adapter you mean one of these? http://www.dofactory.com/net/adapter-design-pattern – Dave Amour Jun 01 '15 at 11:49
  • Yes, I mean Adapter pattern. – Igor Popov Jun 01 '15 at 13:29
  • Ok thanks. Since someone marked this as a duplicate question, does this mean it cannot be answered and I therefore do not need to nominate an accepted answer? – Dave Amour Jun 02 '15 at 03:58
  • @DaveAmour Once a question is closed (in this case because it's a duplicate), no new answers can be added. If there had already been an answer posted, then you could mark it as the accepted answer if you wanted to, however as there aren't any answers currently on this question that isn't an option. – forsvarir Jun 04 '15 at 08:20
  • Ok thanks, Its very strict on this site! – Dave Amour Jun 04 '15 at 09:07

0 Answers0