【问题标题】:Unit test code in catch statement with a throw带有 throw 的 catch 语句中的单元测试代码
【发布时间】:2018-11-30 13:22:13
【问题描述】:

有没有办法在 catch 语句中测试代码,诀窍是 catch 以 throw 结尾,这就是事情的发展方向。所以问题是,有没有办法在运行我的测试方法时避免这种抛出,或者在我的测试中适应它?

下面的方法只是简单地更新值,并且捕获它有一个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();
            }

        }

我现在需要测试catch部分,下面测试

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

【问题讨论】:

    标签: c# unit-testing error-handling try-catch throws


    【解决方案1】:

    不,您无法跳过 catch 的 throw 语句。但是,您可以在单元测试方法上附加ExpectedExceptionAttribute,以便它指示在测试方法执行期间预期会出现异常。您可以在here 阅读有关ExpectedExceptionAttribute 的信息。

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

    另外,我建议不要捕获一般异常,而是定义具有特定异常的属性。

    【讨论】:

      【解决方案2】:

      在 xunit 中还有另一种使用 Record.Exception 的方法,请在下面的链接中查看更多信息:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-15
        • 1970-01-01
        • 1970-01-01
        • 2010-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多