Test Double Sample

From STRIDE Wiki
Jump to: navigation, search

Introduction

This example using a C++ test class cover the application of test doubles. Please read the Using Test Doubles article before proceeding in order to understand the concepts. You may also find it helpful to review the information available here for more details on test double terminology.

Tests Description

TestFunction

This case validates test_func1 function (implemented in s2_testdouble_function.h/c) which in turn depends on depend1 function (in s2_testdouble_depend.h/c). Since the caller (test_func1) and the callee (depend1) are in separate compilation units the depencency is captured for interception (via scl_function pragma in s2_testdouble_function_test.h/c) using "DEFINITION" and "IMPLICIT" options.

CallNoDoubling
Simple case to demonstrate operation where no doubling is used. The true depend1 is used when testing test_func1.
CallWithFake
Dynamically replaces dependency depend1 with a fake fake_depend_1 function, which simply returns a constant.
CallWithMock
Dynamically replaces dependency depend1 with a mock mock_depend_1 function, which validates the passed in parameter and proceeds as the original depend1.

TestFunctionWithDepend

This case, similar to Basic::TestFunction, validates test_func2 function which in turn depends on depend2 function. It differs in that the implementation is done in the same source file (both implemented in s2_testdouble_function_with_depend.h/c). Since the caller (test_func2) and the callee (depend2) are in the same compilation unit the depencency is captured for interception (via scl_function pragma in s2_testdouble_function_with_depend_test.h) using "REFERENCE" and "EXPLICIT" options.

CallNoDoubling
Simple case to demonstrate operation where no doubling is used. The true depend2 is used when testing test_func2.
CallWithFake
Dynamically replaces dependency depend2 with a fake fake_depend_2 function, which simply returns a constant.
CallWithMock
Dynamically replaces dependency depend2 with a mock mock_depend_2 function, which validates the passed in parameter and proceeds as the original depend2.