.net 测试SQL异常

b1zrtrql  于 12个月前  发布在  .NET
关注(0)|答案(1)|浏览(80)

我有一个捕获SqlException的代码。我写了一个测试来处理SQLException,基于下面列出的答案:How to throw a SqlException when needed for mocking and unit testing?

[TestMethod]
 public async Task TestFailJobOnSqlExceptionAsync()
 {      
     _context.Setup(x => x.CallSubOrchestratorAsync()
            .Throws(MakeSqlException());

     var orchestratorFunction = new OrchestratorFunction(_configuration.Object, _loggingRepository.Object);
     await orchestratorFunction.RunOrchestratorAsync(_context.Object, _executionContext.Object);

     _log.Verify(x => x.LogMessageAsync(
                         It.Is<LogMessage>(m => m.Message.StartsWith($"Caught SqlException")),
                         It.IsAny<VerbosityLevel>(),
                         It.IsAny<string>(),
                         It.IsAny<string>()), Times.Never());
 }

 public static SqlException MakeSqlException()
 {
     SqlException exception = null;
     try
     {
         SqlConnection conn = new SqlConnection(@"Data Source=.;Database=GUARANTEED_TO_FAIL;Connection Timeout=1");
         conn.Open();
     }
     catch (SqlException ex)
     {
         exception = ex;
     }
     return (exception);
 }

在运行此命令时,我的测试失败并返回错误
测试方法Services.Tests.TestFailJobOnSqlExceptionAsync引发异常:Microsoft.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关的或特定于示例的错误。服务器未找到或无法访问。请验证示例名称是否正确,以及SQL Server是否配置为允许远程连接。(provider:命名管道提供程序,错误:40 -无法打开到SQL Server的连接)-> System.Web.Win32Exception:系统找不到指定的文件。
我错过了什么?

gkn4icbw

gkn4icbw1#

在编写单元测试时,如果你想Assert某个异常被抛出,你将在测试上面使用[ExpectedException(typeof(Microsoft.Data.SqlClient.SqlException))]属性。
当属性丢失时,测试失败,因为测试框架不理解您期望的异常。

[TestMethod]
[ExpectedException(typeof(Microsoft.Data.SqlClient.SqlException))]
public async Task TestFailJobOnSqlExceptionAsync()
{      
     _context.Setup(x => x.CallSubOrchestratorAsync()
            .Throws(MakeSqlException());

     var orchestratorFunction = new OrchestratorFunction(_configuration.Object, _loggingRepository.Object);
     await orchestratorFunction.RunOrchestratorAsync(_context.Object, _executionContext.Object);

     _log.Verify(x => x.LogMessageAsync(
                         It.Is<LogMessage>(m => m.Message.StartsWith($"Caught SqlException")),
                         It.IsAny<VerbosityLevel>(),
                         It.IsAny<string>(),
                         It.IsAny<string>()), Times.Never());
}

如果你想验证日志记录器是否被调用,你需要在设置过程中编写一个try catch块,直到verify方法。

[TestMethod]
public async Task TestFailJobOnSqlExceptionAsync()
{      

 try 
 {
     _context.Setup(x => x.CallSubOrchestratorAsync()
            .Throws(MakeSqlException());

     var orchestratorFunction = new OrchestratorFunction(_configuration.Object, _loggingRepository.Object);
     await orchestratorFunction.RunOrchestratorAsync(_context.Object, _executionContext.Object);
} catch(Exception)
       {

       }

     _log.Verify(x => x.LogMessageAsync(
                         It.Is<LogMessage>(m => m.Message.StartsWith($"Caught SqlException")),
                         It.IsAny<VerbosityLevel>(),
                         It.IsAny<string>(),
                         It.IsAny<string>()), Times.Never());
}

相关问题