Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking an local object variable #4

Open
Frogli opened this issue Jan 29, 2018 · 4 comments
Open

Mocking an local object variable #4

Frogli opened this issue Jan 29, 2018 · 4 comments

Comments

@Frogli
Copy link

Frogli commented Jan 29, 2018

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?

@barkingfoodog
Copy link
Contributor

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
Copy link
Contributor

@Frogli were you able to get this sorted out?

@Frogli
Copy link
Author

Frogli commented Mar 15, 2018

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

@barkingfoodog
Copy link
Contributor

👍 No worries! Let me know if you get stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants