title: tSQLt - How to do a unit test on code in an error Catch block of code
body: <p>I am trying to implement a UnitTest to test that we log data in a code block that also raises an exception:</p>
  
  <p>So the code looks something like this:</p>
  
  <pre><code>--CREATE TABLE dbo.ErrorLog (ID int IDENTITY (1,1), ErrorDescription Varchar(100))
  GO
  ALTER PROCEDURE dbo.TestError
  AS
  DECLARE @Error varchar(100) = 'one is not equal to two'
  --do some work
  IF 1 &lt;&gt; 2
    BEGIN 
      INSERT dbo.ErrorLog (ErrorDescription) VALUES (@error)
      RAISERROR (@Error,16,1)
      RETURN
    END
  GO
  </code></pre>
  
  <p>And I want to test the Insert of the error into the ErrorLog when the proc I am working on errors. So I wrote my test something like this:</p>
  
  <pre><code>EXEC tSQLt.NewTestClass 'Error'
  GO
  
  CREATE PROCEDURE error.test_GivenError_LogResult
  AS
  --Arrange
  EXEC tSQLt.FakeTable 'dbo.ErrorLog'
  DECLARE @Actual varchar(100)
  DECLARE @Expected Varchar(100) = ('one is not equal to two')
  
  --Act
  --EXEC tSQLt.ExpectException @ExpectedMessage = 'one is not equal to two'
  
  EXEC dbo.TestError
  SELECT top 1 @Actual = ErrorDescription FROM dbo.ErrorLog
  
  --ASSERT
  
  EXEC tSQLt.AssertEquals @Expected, @Actual 
  GO
  
  EXEC tsqlt.Run 'Error'
  </code></pre>
  
  <p>When I execute this test as per above then it reports an error in the proc and does not do the Assert.
  If I include the  tSQLt.ExpectException it passes on the Exception but will not do the AssertEquals on the ErrorLog result.</p>
  
  <p>Is there a way to work around this?</p>