我的控制器中有以下行:
string lTempPath = Path.Combine(Server.MapPath("~/Temp"), lRandomFileName);
问题是服务器不是虚拟的,只能用 getter 访问。
我得到一个
“方法或操作未实现。”
我怎样才能模拟这个服务器?
我创建的测试如下:
[TestCase]
public void PreviewActionShouldGenerateUrlOfPdf()
{
//Arrange
var server = MockRepository.GenerateMock<HttpServerUtilityBase>();
server.Stub(s => s.MapPath("~Temp")).Return("~/Temp");
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
httpContext.Stub(hc => hc.Server).Return(server);
httpContext.Server.Stub(s => s.MapPath("~/Temp")).Return("~/Temp");
var controller = new StudiesController()
{
ReportingService = MockRepository.GenerateMock<IReportingService>(),
SecurityService = MockRepository.GenerateMock<ISecurityService>()
};
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
controller.ReportingService.Stub(rs => rs.GetStudyByGID(new Guid())).Return(new Study());
controller.ReportingService.Stub(rs => rs.ListPractices()).Return(new[] { new Practice(), new Practice() });
controller.SecurityService.Stub(ss => ss.GetUser("")).IgnoreArguments().Return(new User());
controller.ControllerContext.HttpContext = MockRepository.GeneratePartialMock<FakeHttpContext>("http://test.com");
controller.HttpContext.User = new FakePrincipal(new FakeIdentity("test"), new string[0]);
controller.ControllerContext.HttpContext.Stub(x => x.Request).Return(MockRepository.GenerateMock<HttpRequestBase>());
controller.ControllerContext.HttpContext.Request.Stub(x => x.Url).Return(new Uri("http://test.com"));
controller.ReportingService.Stub(
rs =>
rs.GenerateReport(new Study(), new Practice(), new User(), false, ReportGenerationOutputFormat.PDF)).IgnoreArguments().Return(new StudyReportSnapshot());
var content = new ContentResult();
//Act
var result = (ContentResult)controller.Preview(new Guid());
//Assert
Assert.AreEqual(result.Content, content.Content);
}