From eb27f1f0859a814876b605e4095d4cb7a9437b77 Mon Sep 17 00:00:00 2001 From: Mark Abbott Date: Fri, 20 Mar 2015 14:03:20 +0000 Subject: [PATCH] Add example 4 to show how to use the Mock object --- Examples/Example 4/MockProcedureExample.cls | 62 +++++++++++++++++++++ Examples/Example 4/ProcedureToMock.p | 22 ++++++++ Examples/Example 4/ProcedureToTest.p | 45 +++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 Examples/Example 4/MockProcedureExample.cls create mode 100644 Examples/Example 4/ProcedureToMock.p create mode 100644 Examples/Example 4/ProcedureToTest.p diff --git a/Examples/Example 4/MockProcedureExample.cls b/Examples/Example 4/MockProcedureExample.cls new file mode 100644 index 0000000..6d8c999 --- /dev/null +++ b/Examples/Example 4/MockProcedureExample.cls @@ -0,0 +1,62 @@ +/*------------------------------------------------------------------------ + File : MockProcedureExample + Purpose : Example showing how to use OEMock:MockProcedure. + Notes : This example requires that OEMock and OEUnit are already + on the ProPath. + ----------------------------------------------------------------------*/ + +USING Progress.Lang.*. +USING OEMock.*. +USING OEUnit.Assertion.*. + +ROUTINE-LEVEL ON ERROR UNDO, THROW. + +CLASS "Examples.Example 4.MockProcedureExample": + + DEFINE PROTECTED VARIABLE TestHandle AS HANDLE NO-UNDO. + + @Before. + METHOD PUBLIC VOID CreateTestHandle(): + RUN VALUE("Examples/Example 4/ProcedureToTest.p") PERSISTENT SET TestHandle. + END METHOD. + + @After. + METHOD PUBLIC VOID DeleteTestHandle(): + IF VALID-HANDLE(TestHandle) THEN DELETE OBJECT TestHandle. + END METHOD. + + @Test. + METHOD PUBLIC VOID TestGetUserNameIsCalled(): + + DEFINE VARIABLE MessageText AS CHARACTER NO-UNDO. + DEFINE VARIABLE mck AS OEMock.Mock NO-UNDO. + + /* Ask OEMock to generate a Mock object for the required procedure */ + mck = OEMock:MockProcedure("Examples/Example 4/ProcedureToMock.p"). + + /* Set the output parameter value for parameter 'UserName' in the procedure "GetUserName". */ + mck:SetProcedureOutputParameterValue("GetUserName", "UserName", "Mary Jones"). + + /* Set expectation that the "GetUserName" procedure will be run at least once */ + mck:Expect("GetUserName"):CalledAtLeast(1). + + /* Generate the Mock procedure file */ + mck:Generate(). + + /* Run procedure and verify the expected outcome */ + RUN SayHello IN TestHandle(OUTPUT MessageText). + Assert:AreEqual(MessageText, "Hello, Mary Jones"). + + /* Assert that any expectations were met */ + mck:AssertIsSatisfied(). + + FINALLY: + /* Mock object is no longer required so delete */ + IF VALID-OBJECT(mck) THEN DELETE OBJECT mck. + + /* Test completed, tell OEMock to run the clean-up process */ + OEMock:CleanUp(). + END. + END METHOD. + +END CLASS. \ No newline at end of file diff --git a/Examples/Example 4/ProcedureToMock.p b/Examples/Example 4/ProcedureToMock.p new file mode 100644 index 0000000..40fe7eb --- /dev/null +++ b/Examples/Example 4/ProcedureToMock.p @@ -0,0 +1,22 @@ +/*------------------------------------------------------------------------ + File : ProcedureToMock.p + Purpose : Procedure that will be mocked out in tests. + ----------------------------------------------------------------------*/ + +/* *************************** Definitions ************************** */ + +ROUTINE-LEVEL ON ERROR UNDO, THROW. + +/* ******************** Preprocessor Definitions ******************** */ + + +/* *************************** Main Block *************************** */ + +PROCEDURE GetUserName: + + DEFINE OUTPUT PARAMETER UserName AS CHARACTER NO-UNDO. + + /* For the purposes of this example, this is set to a simple string */ + ASSIGN UserName = "Joe Bloggs Jnr.". + +END PROCEDURE. \ No newline at end of file diff --git a/Examples/Example 4/ProcedureToTest.p b/Examples/Example 4/ProcedureToTest.p new file mode 100644 index 0000000..9f15e3e --- /dev/null +++ b/Examples/Example 4/ProcedureToTest.p @@ -0,0 +1,45 @@ +/*------------------------------------------------------------------------ + File : ProcedureToTest.p + Purpose : This file represents a file that is to be tested + ----------------------------------------------------------------------*/ + +/* *************************** Definitions ************************** */ + +ROUTINE-LEVEL ON ERROR UNDO, THROW. + +/* ******************** Preprocessor Definitions ******************** */ + + +/* *************************** Main Block *************************** */ + +PROCEDURE SayHello: + + DEFINE OUTPUT PARAMETER MessageText AS CHARACTER NO-UNDO. + + DEFINE VARIABLE ProcHandle AS HANDLE NO-UNDO. + + RUN VALUE("Examples/Example 4/ProcedureToMock.p") PERSISTENT SET ProcHandle. + + RUN GetUserName IN ProcHandle (OUTPUT MessageText). + + IF VALID-HANDLE(ProcHandle) THEN DELETE OBJECT ProcHandle. + + CASE MessageText: + + WHEN ? THEN + DO: + ASSIGN MessageText = "Hello World". + END. + + WHEN "" THEN + DO: + ASSIGN MessageText = "Hello, person with no name". + END. + + OTHERWISE + DO: + ASSIGN MessageText = "Hello, " + MessageText. + END. + END CASE. + +END PROCEDURE. \ No newline at end of file