Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing MVCContrib TestHelper和User.Identity.Name、Server.MapPath和表单集合_Unit Testing_Rhino Mocks_Mvccontrib_Mvccontrib Testhelper - Fatal编程技术网

Unit testing MVCContrib TestHelper和User.Identity.Name、Server.MapPath和表单集合

Unit testing MVCContrib TestHelper和User.Identity.Name、Server.MapPath和表单集合,unit-testing,rhino-mocks,mvccontrib,mvccontrib-testhelper,Unit Testing,Rhino Mocks,Mvccontrib,Mvccontrib Testhelper,我不熟悉MVCContrib Testhelper和Rhino 我需要帮助对依赖User.Identity.Name、Server.MapPath和表单集合的控制器进行单元测试 我从 var controller = new SubmitController(); var builder = new TestControllerBuilder(); builder.InitializeController(controller); 我找到了用于设置User.Identity.Name的 con

我不熟悉MVCContrib Testhelper和Rhino

我需要帮助对依赖User.Identity.Name、Server.MapPath和表单集合的控制器进行单元测试

我从

var controller = new SubmitController();
var builder = new TestControllerBuilder();
builder.InitializeController(controller);
我找到了用于设置User.Identity.Name的

controller.ControllerContext = TestHelper.MockControllerContext(controller).WithAuthenticatedUser("domain\\username");
此时,在我的控制器中,我现在可以访问User.Identity。接下来的问题是如何设置表单集合变量。背景

builder.Form.Add("testvar","1");
不再有效。似乎现在我必须通过

controller.HttpContext.Request.Form.Add("testvar","1)
这似乎是可行的,但在这一点上,我不再使用上面的构建器(TestControllerBuilder)

然后我不得不模拟服务器,这会引发更多问题。如何继续使用builder,但对httpContext、HttpRequest、Server等使用mock或stub。我有点期待builder会有方法设置HttpRequest、Server等的预期值


感谢您更换控制器的
ControllerContext
,它删除了MVCContrib上下文。试着这样做:

using MvcContrib.TestHelper;
using MvcContrib.TestHelper.Fakes;
using Rhino.Mocks;

...
var builder = new TestControllerBuilder();
builder.Form.Add("testvar", "1"); 
builder.HttpContext.User = new FakePrincipal(new FakeIdentity("UserName"), new string[] { "Role" });
builder.HttpContext.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("Value");
builder.InitializeController(controller);
使用MvcContrib.TestHelper;
使用MvcContrib.TestHelper.Fakes;
使用犀牛;
...
var builder=newtestcontrollerbuilder();
builder.Form.Add(“testvar”,“1”);
builder.HttpContext.User=new FakePrincipal(new FakeIdentity(“用户名”),new string[]{“角色”});
builder.HttpContext.Server.Stub(x=>x.MapPath(Arg.Is.Anything)).Return(“Value”);
生成器.初始化控制器(控制器);

更换控制器的
ControllerContext
时删除了MVCContrib上下文。试着这样做:

using MvcContrib.TestHelper;
using MvcContrib.TestHelper.Fakes;
using Rhino.Mocks;

...
var builder = new TestControllerBuilder();
builder.Form.Add("testvar", "1"); 
builder.HttpContext.User = new FakePrincipal(new FakeIdentity("UserName"), new string[] { "Role" });
builder.HttpContext.Server.Stub(x => x.MapPath(Arg<string>.Is.Anything)).Return("Value");
builder.InitializeController(controller);
使用MvcContrib.TestHelper;
使用MvcContrib.TestHelper.Fakes;
使用犀牛;
...
var builder=newtestcontrollerbuilder();
builder.Form.Add(“testvar”,“1”);
builder.HttpContext.User=new FakePrincipal(new FakeIdentity(“用户名”),new string[]{“角色”});
builder.HttpContext.Server.Stub(x=>x.MapPath(Arg.Is.Anything)).Return(“Value”);
生成器.初始化控制器(控制器);