gpt4 book ai didi

asp.net-mvc - Controller 中的模拟服务器

转载 作者:行者123 更新时间:2023-12-02 10:12:04 26 4
gpt4 key购买 nike

我的 Controller 中有以下行:

string lTempPath = Path.Combine(Server.MapPath("~/Temp"), lRandomFileName);

问题是服务器不是虚拟的,只能通过 getter 来访问。

我得到一个

"The method or operation is not implemented."

我如何模拟这个服务器?

我创建的测试如下:

    [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);

}

最佳答案

您可以在单元测试代码中使用 Mocking 框架,例如 Rhino.Mocks、Moq 或 FakeItEasy

单元测试主体示例如下(本示例使用 Moq)

var homeController = new HomeController();

//create mock of HttpServerUtilityBase
var server = new Mock<HttpServerUtilityBase>();

//set up mock to return known value on call.
server.Setup(x => x.MapPath("~/Temp")).Returns("c:\\temp\\");

var httpContext = new Mock<HttpContextBase>();

httpContext.Setup(x => x.Server).Returns(server.Object);

homeController.ControllerContext = new ControllerContext(httpContext.Object, new RouteData(), homeController);

YourModelName yourModelName = new YourModelName();
yourModelName.lRandomFileName = "zzzz.so";

var result = homeController.YourActionName(yourModelName);

然后您可以断言结果。

希望能给你一些想法

关于asp.net-mvc - Controller 中的模拟服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17332219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com