c# - 带有catch的catch语句中的单元测试代码

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

有没有一种方法可以在catch语句中测试代码,诀窍是catch以throw结尾,这就是问题的解决之道。因此,问题是,是否有一种方法可以避免在运行我的Test方法时抛出该异常,或者在我的测试中使其适应?

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

        }

我需要现在测试捕获部分,在下面进行测试
 [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);
        }

最佳答案

不,您无法跳过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);
    }

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

关于c# - 带有catch的catch语句中的单元测试代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50964652/

相关文章:

unit-testing - 如何Str::shouldReceive? ( mock Illuminate\Support\Str)

javascript - 错误 : Cannot read property of undefiended

c - 为什么我写的代码会出现段错误?

c# - 从 C# 应用程序内部存储空整数值

angular - 如何在 Angular 2 中测试 `Router NavigationEnd Event`?

c# - 创建复杂函数的响应式版本

java - 从 JUnit 命令行获取测试列表

error-handling - Dropzone.js-视觉效果无法正确显示

c# - 过滤数据库

c# - 从第二个 lib 项目绑定(bind)用户控件不起作用 - wpf mvvm