Note that there are some explanatory texts on larger screens.

plurals
  1. POAttempted to read or write protected memory
    primarykey
    data
    text
    <p>I have a sample ASP.NET MVC 3 web application that is following Jonathan McCracken's Test-Drive Asp.NET MVC (great book , by the way) and I have stumbled upon a problem. Note that I'm using MVCContrib, Rhino and NUnit.</p> <pre><code> [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")); } </code></pre> <p>The code above always throws this error:</p> <blockquote> <p>System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p> </blockquote> <p>The controller action code is the following:</p> <pre><code>[HttpGet] public ActionResult Index() { ViewData.Model = Todo.ThingsToBeDone; ViewBag.UserName = HttpContext.User.Identity.Name; return View(); } </code></pre> <p>From what I have figured out, the app seems to crash because of the two assignements in the controller action. However, I cannot see how there are wrong!?</p> <p>Can anyone help me pinpoint the solution to this problem.</p> <p>Thank you.</p> <p><strong>Edit 1</strong></p> <p>I've done some experiments to see what the problem is. When removing the <code>ViewData,Model</code> assignment the problem transcends in <code>Expected result to be of type ViewResult. It is actually of type ViewResult.</code>. The <code>ViewData</code> assignment is so basic that I do not think that it is the problem so I think there is something wrong with either Rhino or MVCcontrib in conjunction with MVC 3.</p> <p>I also have the following test written earlier for the same controller action:</p> <pre><code> [Test] public void ShouldDisplayAListOfTodoItems() { Assert.That(((ViewResult)new TodoController().Index()).ViewData.Model, Is.EqualTo(Todo.ThingsToBeDone)); } </code></pre> <p>This one fails now with <code>System.NullReferenceException : Object reference not set to an instance of an object</code> probably becuase there's no HttpContext set up for this particular test. When removing the <code>ViewBag</code> assignment, everything is ok.</p> <p>Hope that makes the problem more clear.</p> <p><strong>Edit 2</strong></p> <p>When debugging the code after removing the <code>ViewData.Model</code> assignment, it throws a different error: <code>System.NullReferenceException : Object reference not set to an instance of an object.</code> on the <code>ViewBag</code> assignment.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload