Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit test fails because of System.AccessViolationException

I have a series of NUnit tests and some are failing, yet I can't seem to find a reason, and the exception is telling me nothing. This is my case:

    //Controller Action
    [HttpPost]
    [AjaxExceptionHandler]
    [OutputCache(Duration = 0)]
    public PartialViewResult SomeAction(long id)
    {
        try
        {
            var model = _repository.GetModel(id);
            return PartialView(@"MyPartialView", model);
        }
        catch (Exception ex)
        {
            exceptionManager.HandleException(ex, FT_EXCEPTION_POLICY);
            throw;
        }
    }

    //Action Unit Test
    [Test]
    [Category(TestConstants.UnitTest)]
    public void SomeAction_Returns_Expected_View()
    {
        var model = Builder<ViewModel>.CreateNew().Build();

        repository.Stub(it => it.GetModel(Arg<long>.Is.Anything)).Return(model);

        var viewResult = (PartialViewResult)someController.SomeAction(1);
        Assert.AreEqual(@"MyPartialView", viewResult.ViewName);
    }       

Unit Test Exception:

System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

If in my action I pass a null value to the partial view, like so: return PartialView(@"MyPartialView", null); Then the test passes.

Other similar cases fail as well, yet others pass. I have not been able to identify a reason for each.

Can anyone help me identify what is wrong?

Thanks,

EDIT: Ok, I fixed ALL other failing tests and now I have only the ones with the System.AccessViolationException left.

ADDED Setup procedure form my tests:

    [SetUp]
    public void SetUp()
    {
        controllerBuilder = new TestControllerBuilder();

        repository = MockRepository.GenerateStub<ISomeRepository>();

        someController = new SomeController
            (repository);

        controllerBuilder.InitializeController(someController);
    }
like image 887
AJC Avatar asked Sep 10 '11 18:09

AJC


1 Answers

Found an answer... really stupid problem, like most problems in programming. As I always say, if you can't solve it the first couple of hours, then you know its something really really stupid.

Here is where I found the answer, took me a while, but the name of the question didn't help things either:

Attempted to read or write protected memory

In short, I had to replace the MVCContrib Dlls.

Thanks to everyone for the help...

like image 153
AJC Avatar answered Nov 08 '22 16:11

AJC