mercredi 5 février 2020

Why can't I detect this Exception in my Tests

I am writing an Intigration test using nUnit in C#.

My code is:

public async Task<bool> WriteData(Document data, string tableName)
{
   try
   {
      var table = Table.LoadTable(_dynamoDbClient, tableName);
      table.PutItem(data);
      return true;
   }
   catch (Exception e)
   {
     _logger.Info("Failed Writing Record");
     _logger.Info("Error: " + e.Message);
     return false;
   }
}

My Test is:

public void TestToSeeIfWeGetAnExceptionWhenProvidingBadDataToTheDatabase()
{
   // arrange
   var item = new Document
   {
      ["Id"] = "1001",
      ["TransactionID"] = 111111,
      ["StatementType"] = "TestBank"
   };

   _bankStatementTable = "does-not-exist";

   // act / assert

   Assert.Catch<System.Exception>(() => _awsDynamoDbManager.WriteData(item, _bankStatementTable));
}

Because I am passing bad data to the database I expect an exception and I get one.
However, the test fails. I get this message from the test runner:

Expected: instance of <System.Exception>

But was: null

If I run the test in Debug I can see the Catch being hit. What am I missing?

Aucun commentaire:

Enregistrer un commentaire