Asserts that mock object was called within current unit test execution with specified parameter values and, if needed, in a specified order. Fails if mock object were not called during current unit test execution with provide conditions.

Arguments

  • v_PretendObjectName – Name of the mock object. Supports four part object name. SYSNAME data type.
  • v_ExpectedParameterValues - Name of the table (preferably temporary table) that have collection of expected parameter values.
    Table column names must match the names of the pretend object parameters and should be of the same data type as the procedure parameters. It is not required to specify all pretend object parameters, we recommend to provide only those parameters that needs to be verified.  
    Optionally, to check order of a pretend object execution the expectation table should have [ExecutionNumber] column with INT data type. 
    Below are some samples:

          --(A) Verify execution order, together with [@UserName] and [@UserRole]
          --      parameters values for each execution   
          CREATE TABLE #A_ExpectedParameters
           (
                  ExecutionNumber INT,
                  [@UserName]   VARCHAR(50),
                  [@UserRole]   VARCHAR(50)
           )


          --(B) Verify that [@UserName] and [@UserRole] parameters values
          --      provided and ignore order of execution  
          CREATE TABLE #B_ExpectedParameters
           (
                  [@UserName]   VARCHAR(50),
                  [@UserRole]   VARCHAR(50)
           )
         
           --(C) Only verify that all expected [@UserName] parameter values was provided   
          CREATE TABLE #C_ExpectedParameters
           (
                  [@UserName]   VARCHAR(50)
           )


     
  • v_Debug - Default value is 0, when set to 1 this assert will output Actual and Expected results. BIT data type. 
  • v_UserMessage – Message to report when assertion fails, NVARCHAR(MAX)

 

Note: If you are planning to run unit test manually, you must use DBTD_UNIT_TEST hint procedure to explicitly define a unit test procedure as the unit test.

Examples

SQL Server

 

/*Create inner procedure that will be called within outer scope*/

CREATE PROC AddNewUser

       @UserName     VARCHAR(50),

       @UserRole     VARCHAR(50)

AS

BEGIN

       --Do something

       PRINT @UserName

       PRINT @UserRole

END

GO

 

/*create primary outer procedure*/

CREATE PROC SetupDefaultUsers

AS

BEGIN

       EXEC AddNewUser @UserName = 'Kelly',     @UserRole = 'Administrator'

       EXEC AddNewUser @UserName = 'Bill',      @UserRole = 'Reporting Team'

       EXEC AddNewUser @UserName = 'Michael',   @UserRole = 'Developer'

END

GO

 

/*create unit test procedure*/

CREATE PROCEDURE UT_USERTESTS_AddNewUser

AS

BEGIN

       --make sure that this test is the part of the USERTESTS suite

       EXEC DBTD_UNIT_TEST 'USERTESTS'

 

       --create table that will hold expected results

       CREATE TABLE #ExpectedParameters

       (

              ExecutionNumber INT,

              [@UserName]   VARCHAR(50),

              [@UserRole]   VARCHAR(50)

       )

 

       --populate expected results

       INSERT INTO #ExpectedParameters

              (ExecutionNumber,[@UserName],[@UserRole])

              VALUES  (1, 'Kelly', 'Administrator'),

                      (2, 'Bill',   'Reporting Team'),

                      (3, 'Michael','Developer')

 

       --create mock for inner procedure

       EXEC DBTD_CREATE_MOCK_PROCEDURE 'AddNewUser', NULL, NULL, NULL

      

       --call primary busines logic

       EXEC SetupDefaultUsers

 

       --assert that inner procedure were called within primary logic

       --and that it were called expected number of times with expected parameters

       EXEC DBTD_ASSERT_MOCK_CALLED_WITH_PARAMS

              @v_PrependObjectName       = 'AddNewUser',

              @v_ExpectedParameterValues = '#ExpectedParameters',  

              @v_Debug                   = 0,

              @v_UserMessage             = 'We have issues'

END

GO

 

--run just one unit test alone to verify results

--note that we will explicitly use transactions

--because we run unit test outside of the DBTestDriven framework

BEGIN TRAN

EXEC UT_USERTESTS_AddNewUser

ROLLBACK

GO

   

See Also