0

Is there a way to test code in catch statement, the trick is the catch ends with a throw and this is where things go side ways. So the question is,is there a way to avoid that throw when running my Test method or maybe accomodate it on my test?

The method below just simply updates values and on catch it has a throw

public void UpdateCase(LinqToSQLModelName record, IProgress<string> statusReporter)
        {

            try
            {
                statusReporter.Report(record.Reference);
                _service.PushToService(record);
                record.ProcessedDate = DateTime.Now;
                record.StatusId = (int)Status.Processed;
            }
            catch (Exception ex)
            {

                record.StatusId = (int)Status.Fail;
                record.ErrorExceptions = ex.StackTrace;
                record.ProcessedDate = DateTime.Now;
                Result = false;

                throw ex;//How do dodge this when running my test/ Accomodate it on my Test
            }
            finally
            {
                _db.SubmitChanges();
            }

        }

I need need to test the catch part now, test below

 [TestMethod()]
        public void UpdateCaseErrorTest()
        {

            var repository = new Mock<IDataContext>();
            var errorLoggerRepository = new Mock<IExceptionLogger>();
            var serviceRepository = new Mock<IServiceInterface>();
            repository.Setup(m => m.Get()).Returns(new 
            ManualWithDrawDataContext());

            repository.Setup(m => m.GetCouncilRefundRecord())
             .Returns(new LinqToSQLModelName
             {
                 refrence= "10",
                 Reason = "Reason",
                 DateCaptured = DateTime.Now,

             });

            var sut = new DataContext(repository.Object.Get(), serviceRepository.Object, errorLoggerRepository.Object);

             sut.UpdateCase(repository.Object.GetCouncilRefundRecord(), null);//This null allows me to go to the catch

             Assert.IsFalse(sut.Result);
        }
Anele Ngqandu
  • 115
  • 1
  • 17

2 Answers2

3

No, there is no way you can skip the throw statement of catch. However, you can attach the ExpectedExceptionAttribute over unit test method so that it indicates that an exception is expected during test method execution. You can read about ExpectedExceptionAttribute at here.

    [TestMethod()]
    [ExpectedException(typeof(Exception))]
    public void UpdateCaseErrorTest()
    {

        var repository = new Mock<IDataContext>();
        var errorLoggerRepository = new Mock<IExceptionLogger>();
        var serviceRepository = new Mock<IServiceInterface>();
        repository.Setup(m => m.Get()).Returns(new 
        ManualWithDrawDataContext());

        repository.Setup(m => m.GetCouncilRefundRecord())
         .Returns(new LinqToSQLModelName
         {
             refrence= "10",
             Reason = "Reason",
             DateCaptured = DateTime.Now,

         });

        var sut = new DataContext(repository.Object.Get(), serviceRepository.Object, errorLoggerRepository.Object);

         sut.UpdateCase(repository.Object.GetCouncilRefundRecord(), null);//This null allows me to go to the catch

         Assert.IsFalse(sut.Result);
    }

Also, I would suggest that instead of catching general exception, define the attribute with specific exception.

user1672994
  • 10,509
  • 1
  • 19
  • 32
0

There is another way of doing this with Record.Exception in xunit and see more below the link:

How to test for exceptions thrown using xUnit, SubSpec and FakeItEasy

DerInder
  • 31
  • 5