tSQLt - Database Unit Testing for SQL Server

Database Unit Testing for SQL Server

  • Home
  • Docs
    • Documents
    • Quick Start
    • Articles
  • Training
  • Downloads
tSQLt » Documents » Expectations » ExpectException

ExpectException

Syntax

tSQLt.ExpectException 
                     [  [@ExpectedMessage= ] 'expected error message']
                     [, [@ExpectedSeverity= ] 'expected error severity']
                     [, [@ExpectedState= ] 'expected error state']
                     [, [@Message= ] 'supplemental fail message']
                     [, [@ExpectedMessagePattern= ] 'expected error message pattern']
                     [, [@ExpectedErrorNumber= ] 'expected error number']

Arguments

[@ExpectedMessage = ] ‘expected error message’
Optional. The expected error message. The test fails if an error with a different message is thrown.
[@ExpectedSeverity = ] ‘expected error severity’
Optional. The expected error severity. The test fails if an error with a different severity is thrown.
[@ExpectedState = ] ‘expected error state’
Optional. The expected error state. The test fails if an error with a different state is thrown.
[@Message = ] ‘supplemental fail message’
Optional. Supplemental information to clarify the test’s intent. This is displayed in case of a failure.
[@ExpectedMessagePattern = ] ‘expected error message pattern’
Optional. A pattern describing the expected error message. The test fails if an error with a message not matching this pattern is thrown.
[@ExpectedErrorNumber = ] ‘expected error number’
Optional. The expected error number. The test fails if an error with a different number is thrown.

Return Code Values

Returns 0

Errors Raised

Raises a ‘failure’ error if an error matching the expectation is not raised.

Result Sets

None

Overview

tSQLt.ExpectException marks the point in the test after which an error should be raised. All parameters are optional. Independent of the supplied parameters, the test fails if after the tSQLt.ExpectException call no error is raised. Passing in a NULL in any parameter has the same effect as omitting that parameter.

The parameters allow to constrain the expected exception further.

There can be only one call to tSQLt.ExpectException per test. However, a call to tSQLt.ExpectException can follow a call to tSQLt.ExpectNoException.

Examples

There are two main call patterns:

EXEC tSQLt.ExpectException @ExpectedMessage = 'Some Expected Message', @ExpectedSeverity = NULL, @ExpectedState = NULL;

and

EXEC tSQLt.ExpectException @ExpectedMessagePattern = '%Part of Expected Message%', @ExpectedSeverity = NULL, @ExpectedState = NULL;

Example: Using tSQLt.ExpectException to check that correct error is raised

CREATE PROCEDURE PurgeTableTests.[test dbo.PurgeTable rejects not existing table]
AS
BEGIN
  EXEC tSQLt.ExpectException @Message = 'Table dbo.DoesNotExist not found.', @ExpectedSeverity = 16, @ExpectedState = 10;
  EXEC dbo.PurgeTable @TableName='dbo.DoesNotExist';
END;
GO

See Also

  • ExpectException
  • ExpectNoException
Share and Enjoy:
  • FacebookFacebook
  • TwitterTwitter
  • LinkedInLinkedIn
  • RedditReddit
  • StumbleUponStumbleUpon
  • TechnoratiTechnorati
  • PrintPrint

Navigation

  • Full user guide
  • Why you should use the tSQLt framework
  • How to get more out of tSQLt
  • Join the conversation
  • Downloads
  • Articles
  • Release Notes
  • New Logo
  • Training
  • Sponsors & Contributors
  • Why you should unit test SQL Server Code
  • Documents
    • Quick Start
    • tSQLt Tutorial
    • tSQLt Keyboard Shortcuts
    • Test Creation and Execution
      • NewTestClass
      • DropClass
      • RunAll
      • Run
      • RenameClass
    • Assertions
      • AssertNotEquals
      • AssertEmptyTable
      • AssertEquals
      • AssertEqualsString
      • AssertEqualsTable
      • AssertObjectExists
      • AssertResultSetsHaveSameMetaData
      • Fail
      • AssertObjectDoesNotExist
      • AssertEqualsTableSchema
      • AssertLike
    • Expectations
      • ExpectException
      • ExpectNoException
    • Isolating Dependencies
      • ApplyConstraint
      • FakeTable
      • SpyProcedure
      • FakeFunction
      • RemoveObjectIfExists
      • ApplyTrigger
      • RemoveObject

Links

  • tSQLt on GitHub
  • tSQLt Mailing List
  • twitter hashtag (#tsqlt)
  • tSQLt tag on stackoverflow.com
  • SQL Server Community on Slack (#tsqlt)
  • #tSQLtLive on YouTube

Sponsors

sqlity.net
redgate.com

A sqlity.net llc Web Property. | ©2010 - 2020, All Rights Reserved. | Privacy Policy | Terms of Use