0

I have a simple action:

[HttpPost]
public virtual ActionResult AddVote(string id, sbyte value)
{
   //...
   if (somethingIsWrong)
      ModelState.AddModelError("", "SomethingIsWrong");
   //...
}

Now I want to test whether ModelState is valid/invalid:

[Fact]
public void AddVotePostTest()
{
   var controller = new VoteController();
   controller.AddVote("someId", 1);

   Assert.True(controller.ModelState.IsValid); //AccessViolationException here
}

But I get AccessViolationException at the point where I call controller.ModelState.IsValid.

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • This might help: http://stackoverflow.com/questions/286124/how-can-i-test-modelstate – Faust Oct 07 '12 at 07:29
  • Check this : [Test Driven Development with ASP.NET MVC](http://www.singingeels.com/Articles/Test_Driven_Development_with_ASPNET_MVC.aspx) – Yasser Shaikh Oct 07 '12 at 10:57

1 Answers1

1

The error was caused by the fact System.Web.Mvc 3 was referenced in my test project. I have referenced System.Web.Mvc 4 and the problem gone

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • 1
    I had the same problem once. It's all because MVC3 is also .NET 4.0, so you see the four and you immediately assume it's MVC4 ;) – Michal B. Oct 12 '12 at 07:18