开发者

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().Buil开发者_如何学God();

        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);
    }


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...


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

This error comes in a field when playing with managed/unmanaged code, especially allocating unmanaged resources in managed code and freed them too early, when managed code still exists to access resources being released by the operating system.

You are using IntPtr inpropertly, or have a memory leak, or declaration of an extern COM/Win32 functions is not correct, f/e in [DllImport(...)] attributes.

Look into the code more criticall & presizely

exceptionManager.HandleException(ex, FT_EXCEPTION_POLICY);


It could be a threading / race condition problem.

You are not creating the controller, somecontroller in the test. Therefore, many tests are using the same instance of the controller, this can lead to memory corruption errors.

Try creating and disposing the controller inside each test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜