尝试使用 MvcContrib 测试助手和 Rhino Mocks 3.5 存根 Server.MapPath [英] Trying to stub Server.MapPath with MvcContrib Test helpers and Rhino Mocks 3.5
问题描述
我正在使用 MvcContrib 的测试助手和 Rhino Mocks 3.5 来测试 ASP.NET MVC 操作方法.我像这样构建我的假控制器:
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);
所以我得到了一个包含虚假 HTTP 服务器等的虚假控制器
So I get a fake controller that contains fake HTTP Server etc.
然后我试图像这样存根 Server.MapPath 方法
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/");
但在我测试的方法中,对 Server.MapPath("/APP_DATA/Files/") 的调用返回 null.
but in my method under test the call to Server.MapPath("/APP_DATA/Files/") returns 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);
我缺少什么?
推荐答案
旧帖子,但我正在寻找这个,我找到了一个解决方案,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.
_builder.HttpContext.Server.Stub(s => s.MapPath("~/" + filepath)).Repeat.Once().Return(mapedPath);
这篇关于尝试使用 MvcContrib 测试助手和 Rhino Mocks 3.5 存根 Server.MapPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!