omnistap icon indicating copy to clipboard operation
omnistap copied to clipboard

Mocking an local object variable

Open Frogli opened this issue 7 years ago • 4 comments

A question about the mocking mechanism in OmnisTAP: I noticed that for every test class in the $setup gets a line where a mock of the production class

Do $cinst.$mock($objects.oTree) Returns iorMock

This got me confused how the mocking system is working. In my perception you only mock the objects which you do not want to test and want to communicate to eg a database. I looked at the example and there also the production class is mocked, certain methods are mocked, the method to test is called and then the mocks are asserted.

Let say I want to mock a object used as a local variable in an object. How do I do that? With UnitTest framwork based on OmnisUnitTest. I do something like this:

Do $clib.$classes.$add(kObjectclass,'oTreeOrg') Returns newObjectRef
Do newObjectRef.$classdata.$assign($libs.OmniHisSql.$classes.oTree.$classdata)
Do $libs.OmniHisSql.$objects.oTree.$classdata.$assign($clib.$classes.oTree.$classdata)

; Do some tests
;
Do loForest.$getTreeCount() Returns liTreeCount

; Do some assertions

Do $libs.OmniHisSql.$classes.oTree.$classdata.$assign($clib.$classes.oTreeOrg.$classdata)
Do $clib.$classes.$remove($clib.$classes.oTreeOrg.$ref())

I presume that this should be possible with OmnisTAP, but I did not get it working yet. Below an example of my failed attempt.

Do $cinst.$mock($objects.oTree) Returns loTreeMock
Do $objects.oForest.$newref() Returns lorForest

;  Set Mock properties
Do loTreeMock.$mock("$isTree").$expect(kTrue).$return_boolean(kTrue)
Do lorForest.$getTreeCount() Returns liTreeCount
Do ioTAP.$is_number(liTreeCount,liTreeCount,'Get the number of trees in the Forest')
Do $cinst.$asserts()

Could you help me with this. What am I missing?

Frogli avatar Jan 29 '18 09:01 Frogli

In my perception you only mock the objects which you do not want to test and want to communicate to eg a database.

New test classes generate a mocked object for use when testing. By default, only $construct and $destruct are overridden. When calling any other method you'll get the real, production code. We generate a mock expecting that you'll want to mock other methods on class when testing glue code.

Let say I want to mock a object used as a local variable in an object. How do I that? I believe you're asking about using a mock when the local variable is constructed during a method. OmnisTAP doesn't handle this. Instead, we would move the construct to a protected getter, then mock that. Here is an example:

Do $cinst.$mock($objects.oTree) Returns loTreeMock
Do $cinst.$mock($objects.oForest) Returns lorForest
Do lorForest.$mock("$_getTree").$return_objref(lorTreeMock)

;  Set Mock properties
Do loTreeMock.$mock("$isTree").$expect(kTrue).$return_boolean(kTrue)
Do lorForest.$getTreeCount() Returns liTreeCount
Do ioTAP.$is_number(liTreeCount,liTreeCount,'Get the number of trees in the Forest')
Do $cinst.$asserts()

Instead of calling $objects.oTree.$newref() in $getTreeCount(), you would call $cinst.$_getTree().

barkingfoodog avatar Jan 29 '18 13:01 barkingfoodog

@Frogli were you able to get this sorted out?

barkingfoodog avatar Mar 15 '18 12:03 barkingfoodog

@barkingfoodog No I am sorry, unfortunately not. I hope I can look at it this week.

Frogli avatar Mar 15 '18 14:03 Frogli

:thumbsup: No worries! Let me know if you get stuck.

barkingfoodog avatar Mar 15 '18 14:03 barkingfoodog