How to handle exception in NUnit

I wrote a unit test class in C # for my MVC project.

The testing method is as follows.

[Test] public void To_Add_DocumentStatusIsNull_ThrowsInvalidOperationException_ServiceTest() { try { _IDocumentStatusRepositoryMock = new Mock<IDocumentStatusRepository>(); _unitOfWorkMock = new Mock<IUnitOfWork>(); DocumentStatusService documentStatusService = new DocumentStatusService(_unitOfWorkMock.Object, _IDocumentStatusRepositoryMock.Object); DocumentStatus documentStatus; documentStatus = null; _IDocumentStatusRepositoryMock.Setup(m => m.Add(documentStatus)); documentStatusService.Add(documentStatus); Assert.Pass(); } catch (Exception e) { throw new Exception(e.Message); } } 

And the service method follows

  public virtual void Add(TEntity entity) { try { if (entity == null) { throw new ArgumentNullException("entity"); } _repository.Add(entity); } catch (Exception e) { throw new Exception(e.Message); } } 

Now this test method just failed because of the class of service called by ArgumentNullException. So how to handle ArgumentNullException or How to make this test pass?

Please help someone help

+6
source share
3 answers

If you are trying to verify that an ArgumentNullException running (that: it is currently missing). then it sounds like you want:

 [Test, ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null. Parameter name: entity")] public void To_Add_DocumentStatusIsNull_ThrowsInvalidOperationException_ServiceTest() { _IDocumentStatusRepositoryMock = new Mock<IDocumentStatusRepository>(); _unitOfWorkMock = new Mock<IUnitOfWork>(); DocumentStatusService documentStatusService = new DocumentStatusService(_unitOfWorkMock.Object, _IDocumentStatusRepositoryMock.Object); DocumentStatus documentStatus; documentStatus = null; _IDocumentStatusRepositoryMock.Setup(m => m.Add(documentStatus)); documentStatusService.Add(documentStatus); } 

...

 public virtual void Add(TEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } _repository.Add(entity); } 
+5
source

I guess: look at the code that this unit test should not pass. Adding NULL to the list is in most cases not the intended behavior.

I see 2 options: A) You should add try / catch to the test method.

 try { _IDocumentStatusRepositoryMock.Setup(m => m.Add(documentStatus)); documentStatusService.Add(documentStatus); } catch (Exception ) { Assert.Fail(); // or nothing is expected behaviour } 

B) Remove the try / catch block from the test method so that you do not swallow the exception. (Every test that fails, or Assembler or throws a random exception, automatically passes)

+1
source

Testing for ArgumentNullException

If you remove laid-back

 catch (Exception e) { throw new Exception(e.Message); } 

from your test code (the current catch loses the error context and breaks the stack trace, see below), your test can be as simple as transferring a call to Assert.Throws<ArgumentNullException>() :

 [Test] public void PassingANullEntityToAddMustThrowArgumentNullException() { var documentStatusService = new DocumentStatusService(...); Assert.Throws<ArgumentNullException>(() => documentStatusService.Add(null)); } 

Re: Your exception handler

In your service code, never catch the exception and do not collapse it the way you did, as this will lose the stack trace (for example, _repository.Add(entity); may also be _repository.Add(entity); .). You also do not add any value by throwing e.Message , as it is already in the original exception (with additional information such as stack trace and internal exception)

Bad:

  catch (Exception e) { throw new Exception(e.Message); } 

Better: if you catch and pritronet with some value, wrap the original as an internal exception:

  catch (SqlException ex) { throw new Exception("Some value add here", ex); } 

or if you just intercept and allow distribution:

  catch (SqlException) { // Do some logging throw; } 

It would be best to allow the exception to be thrown if you are not adding a value or handling it.

-1
source

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


All Articles