Checks that pretend object exists in the database, verifies that it is correctly registered in the DBTD_TBL_PRETEND_OBJECT table and that it was automatically created by the DBTestDriven Framework. Report failure in any other case.

Arguments

  • v_PretendObjectName – Name of the mock object. Supports four part object name. SYSNAME 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 original business logic*/

CREATE PROC Add_Client

       @Client_Name VARCHAR(100) = 'The Best Client'

AS

BEGIN

       DECLARE @Now DATETIME = GETDATE()

       PRINT 'I am an original business logic';

END

GO

 

/*Create the unit test*/

CREATE PROC UT_SAMPLES_StubForAddClientProc

AS

BEGIN

       EXEC DBTD_UNIT_TEST 'SAMPLES'

       DECLARE @v_StubProcName VARCHAR(128) = 'STUB_Add_Client_ForTestProject',

                     @SQL NVARCHAR(MAX)

 

       --If check the database before creating a stub, we will find that

       --there is no procedure in the database with following error messages:

       -- (1) Procedure DOES NOT EXISTS in the database.

       --     Cannot find stub procedure for our test project.

       --     Cannot find Stored Procedure "[DBO].[STUB_ADD_CLIENT_FOR_TEST_PROJECT]"

       -- (2) This procedure did NOT AUTOMATICALLY CREATED BY DBTD FRAMEWORK.

       --     Cannot find stub procedure for our test project.

       -- (3) This procedure did NOT REGISTERED as valid MOCK Object.

       --     Cannot find stub procedure for our test project.

       SET @SQL = 'EXEC DBTD_ASSERT_STUB_EXISTS @v_PrependObjectName = '''

              + @v_StubProcName

              + ''', @v_UserMessage = ''Cannot find stub procedure for our test project.'''

       EXEC DBTD_ASSERT_WILL_FAIL

              @SQL,

              'we expected assert to fail because stub vere not created yet'   

 

       --Now lets create a stub

       EXEC DBTD_CREATE_STUB_PROCEDURE

              @v_StubProcName = @v_StubProcName,

              @v_OriginalProcName = 'Add_Client',

              @v_StubSQL = 'PRINT ''I am STUB based on Add_Client procedure'';';

 

       --lets try to execute it

       EXEC @v_StubProcName

 

       --After creating a stub, there is no mo errors reported by assert

       EXEC DBTD_ASSERT_STUB_EXISTS

              @v_PrependObjectName = @v_StubProcName,

              @v_UserMessage = 'Cannot find stub procedure for our test project.'   

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_SAMPLES_StubForAddClientProc

ROLLBACK

GO

 

See Also