NUnit test failed due to System.AccessViolationException

I have a series of NUnit tests, some of them fail, but I cannot find the reason, and the exception tells 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 often indicates that another memory is corrupted.

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

Other similar cases fail, but others pass. I could not determine the reason for each.

Can someone help me determine what is wrong?

Thanks,

EDIT: Ok, I fixed ALL other failed tests, and now I only have those with a System.AccessViolationException .

ADDED Installation procedure generates my tests:

  [SetUp] public void SetUp() { controllerBuilder = new TestControllerBuilder(); repository = MockRepository.GenerateStub<ISomeRepository>(); someController = new SomeController (repository); controllerBuilder.InitializeController(someController); } 
+6
source share
3 answers

The answer is found ... a really stupid problem, like most programming problems. As I always say, if you can’t solve it in the first couple of hours, then you know that it’s really really stupid.

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

Attempted to read or write protected memory

In short, I had to replace the MVCContrib DLL.

Thank you all for your help ...

+3
source

System.AccessViolationException: attempt to read or write protected memory. This often indicates that another memory is corrupted.

This error occurs when playing with managed / unmanaged code, especially when allocating unmanaged resources in managed code and releasing them too early when the managed code still exists to access resources that are freed by the operating system.

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

Look at the code for more criticism and the presidium

 exceptionManager.HandleException(ex, FT_EXCEPTION_POLICY); 
0
source

This may be a thread / race issue.

You do not create a controller, somecontroller in the test. Therefore, many tests use the same controller instance; this can lead to memory corruption errors.

Try creating and disposing of a controller inside each test.

0
source

Source: https://habr.com/ru/post/897051/


All Articles