How to use Rhino Mocks to Mock an HttpContext.Application

后端 未结 2 1217
长情又很酷
长情又很酷 2020-12-29 00:38

I\'m new to Mocking frameworks and have started using RhinoMocks to assist with my MVC App Unit Testing.

I\'m using Scott Hanselmanns MVC Mock Helper to assist in mo

2条回答
  •  灰色年华
    2020-12-29 01:25

    you could use the below for Moq. It took me awhile how to mock the HttpApplication, and the appState.Object is the return method duh!

    public static HttpContextBase FakeHttpContext()
        {
            var context = new Mock();
            var request = new Mock();
            var response = new Mock();
            var session = new FakeHttpSessionState();
            var server = new Mock();
            var appState = new Mock();
    
            context.Setup(ctx => ctx.Request).Returns(request.Object);
            context.Setup(ctx => ctx.Response).Returns(response.Object);
            context.Setup(ctx => ctx.Session).Returns(session);
            context.Setup(ctx => ctx.Server).Returns(server.Object);
            context.Setup(ctx => ctx.Application).Returns(appState.Object);
    
            //emulate session (HttpContext.Current.Session) 
            var contx = new HttpContext(new MyApp.NUnit.Tests.Fakes.FakeHttpWorkerRequest());
            contx.Items["AspSession"] = CreateSession();
    
            HttpContext.Current = contx;
    
            return context.Object;
        }
    

提交回复
热议问题