Skip to content

Commit

Permalink
Add example 4 to show how to use the Mock object
Browse files Browse the repository at this point in the history
  • Loading branch information
msabbott committed Mar 20, 2015
1 parent 634b81a commit eb27f1f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Examples/Example 4/MockProcedureExample.cls
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 22 additions & 0 deletions Examples/Example 4/ProcedureToMock.p
Original file line number Diff line number Diff line change
@@ -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.
45 changes: 45 additions & 0 deletions Examples/Example 4/ProcedureToTest.p
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit eb27f1f

Please sign in to comment.