我有一个示例ASP.NET MVC 3 Web应用程序,遵循Jonathan McCracken的Test-Drive Asp.NET MVC(顺便说一句好书),我偶然发现了一个问题.请注意,我使用的是MVCContrib,Rhino和NUnit.
[Test]
public void ShouldSetLoggedInUserToViewBag() {
var todoController = new TodoController();
var builder = new TestControllerBuilder();
builder.InitializeController(todoController);
builder.HttpContext.User = new GenericPrincipal(new GenericIdentity("John Doe"), null);
Assert.That(todoController.Index().AssertViewRendered().ViewData["UserName"], Is.EqualTo("John Doe"));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码总是抛出这个错误:
System.AccessViolationException:尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
控制器操作代码如下:
[HttpGet]
public ActionResult Index() {
ViewData.Model = Todo.ThingsToBeDone;
ViewBag.UserName = HttpContext.User.Identity.Name;
return View();
}
Run Code Online (Sandbox Code Playgroud)
根据我的想法,应用程序似乎崩溃,因为控制器操作中的两个分配.但是,我看不出有多么错误!?
任何人都可以帮我找出解决这个问题的方法.
谢谢.
编辑1
我做了一些实验来看看问题是什么.删除ViewData,Model分配时,问题会超越Expected result to be of type ViewResult. It is actually of type ViewResult..该ViewData分配是如此基本,我不认为是这样,我认为这个问题有什么问题或者犀牛或MVCcontrib结合MVC 3.
我之前为同一控制器操作编写了以下测试:
[Test]
public void ShouldDisplayAListOfTodoItems() …Run Code Online (Sandbox Code Playgroud)