我目前正在使用 MoqSystem.Web.HttpApplication
在 C# 中编写自动化测试:
public FakeHttpApplication CreateBaseMocks()
{
MockContext = new Mock<HttpContextBase>();
MockRequest = new Mock<HttpRequestBase>();
MockResponse = new Mock<HttpResponseBase>();
MockSession = new Mock<HttpSessionStateBase>();
MockServer = new Mock<HttpServerUtilityBase>();
MockContext.Setup(ctx => ctx.Request).Returns(MockRequest.Object);
MockContext.Setup(ctx => ctx.Response).Returns(MockResponse.Object);
MockContext.Setup(ctx => ctx.Session).Returns(MockSession.Object);
MockContext.Setup(ctx => ctx.Server).Returns(MockServer.Object);
HttpContextBase = MockContext.Object;
HttpRequestBase = MockRequest.Object;
HttpResponseBase = MockResponse.Object;
MockRequest.Setup(r => r.Headers).Returns(new NameValueCollection());
return this;
}
public FakeHttpApplication SetupNormalRequestValues()
{
//Context.User
MockUser = new Mock<IPrincipal>();
MockIdentity = new Mock<IIdentity>();
MockContext.Setup(context => context.User).Returns(MockUser.Object);
MockUser.Setup(context => context.Identity).Returns(MockIdentity.Object);
//Request
MockRequest.Setup(request => request.InputStream).Returns(new MemoryStream());
//Response
MockResponse.Setup(response => response.OutputStream).Returns(new MemoryStream());
return this;
}
但是,考虑到大多数内部对象和属性仍然存在,以这种方式设置对象还有很多不足之处null
。有谁知道更好的方法来做到这一点?我正在寻找一种方法来实例化一个更逼真的HttpApplication
对象并且不介意有点复杂。