Asserts that stub 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.
/*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 CallStubProcedures
AS
BEGIN
EXEC STUB_AddNewUser @UserName = 'Kelly', @UserRole = 'Administrator'
EXEC STUB_AddNewUser @UserName = 'Bill', @UserRole = 'Reporting Team'
EXEC STUB_AddNewUser @UserName = 'Michael', @UserRole = 'Developer'
END
GO
/*create unit test procedure*/
CREATE PROCEDURE UT_USERTESTS_AddNewUserO
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 stub for inner procedure
EXEC DBTD_CREATE_STUB_PROCEDURE 'AddNewUser','STUB_AddNewUser', NULL
--call primary busines logic
EXEC CallStubProcedures
--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 = 'STUB_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_AddNewUserO
ROLLBACK
GO
See Also