尝试使用 MvcContrib 测试助手和 Rhino Mocks 3.5 来存根 Server.MapPath
我正在使用 MvcContrib 的测试助手和 Rhino Mocks 3.5 来测试 ASP.NET MVC 操作方法。我像这样构建我的假控制器:
var _builder = new TestControllerBuilder();
_builder.InitializeController(_controller);
所以我得到一个包含假 HTTP Server 等的假控制器。
然后我尝试像这样存根 Server.MapPath 方法
controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/APP_DATA/Files/");
,但在我的测试方法中调用 Server.MapPath("/ APP_DATA/Files/") 返回 null。
这是测试
const string STOCK_NUMBER_ID = "1";
const string FULL_FILE_PATH = "App-Data/Files";
var controller = CreateStockController();
_uploadedFileTransformer.Stub(x => x.ImageBytes).Return(new byte[10]);
_uploadedFileTransformer.Stub(x => x.ConvertFileToBytes(FULL_FILE_PATH)).Return(true);
controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/App_Data/Files/");
controller.AddImage(Guid.NewGuid(), STOCK_NUMBER_ID);
我缺少什么?
I'm using MvcContrib's test helpers and Rhino Mocks 3.5 to test an ASP.NET MVC action method. I build my fake controller like so:
var _builder = new TestControllerBuilder();
_builder.InitializeController(_controller);
So I get a fake controller that contains fake HTTP Server etc.
I'm then trying to stub the Server.MapPath method like so
controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/APP_DATA/Files/");
but in my method under test the call to Server.MapPath("/APP_DATA/Files/") returns null.
This is the test
const string STOCK_NUMBER_ID = "1";
const string FULL_FILE_PATH = "App-Data/Files";
var controller = CreateStockController();
_uploadedFileTransformer.Stub(x => x.ImageBytes).Return(new byte[10]);
_uploadedFileTransformer.Stub(x => x.ConvertFileToBytes(FULL_FILE_PATH)).Return(true);
controller.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("/App_Data/Files/");
controller.AddImage(Guid.NewGuid(), STOCK_NUMBER_ID);
What I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
旧帖子,但我正在寻找这个,我找到了一个解决方案,MvcContrib 的 TestHelper 可能已经修复了它,因为对我来说它正在工作。
Old post but I was searching for this and I found a solution, MvcContrib's TestHelper probably got it fixed because for me it's working.
看起来这是 MVCContrib 中的一个错误(至少对于我的机器上的 -- v1.0.0.0)。当设置控制器上下文时,它使用Rhino.Mocks记录/重放模式,但是(这就是错误),它不会将HttpServer模拟置于重放模式。它将其他所有内容置于重播模式,但不是那个。
因此,一个快速解决方法是:
作为测试“安排”部分的一部分。然后就可以正常工作了。
Looks like this is a bug in MVCContrib (at least with what I have on my machine -- v1.0.0.0). When setting up the controller context, it's using Rhino.Mocks record/replay mode, but (and this is the bug), it doesn't put the HttpServer mock into replay mode. It puts everything else in replay mode but not that one.
So a quick fix is to do:
As part of your "arrange" section of your test. Then it works fine.