Skip to content

Commit 97da6fa

Browse files
committed
improve static mock docc
1 parent 4ea7b2c commit 97da6fa

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

docs/recipes/static-mocking.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!--
22
{
33
"title": "Mocking Static Functions",
4-
"id": "static-mocking",
5-
"categories": ["testing"],
4+
"id": "mocking-static-functions-lucee",
5+
"categories": ["lucee", "testing"],
66
"description": "How to mock static functions in Lucee for better testability without unnecessary wrappers.",
77
"keywords": [
88
"Static Functions",
@@ -49,7 +49,7 @@ dump(testInstance.myStaticFunction()); // Outputs: "static"
4949

5050
## Mocking Static Functions
5151

52-
One key advantage of this behavior is that **static functions can be mocked just like instance functions**.
52+
One key advantage of this behavior is that **static functions can be mocked just like instance functions**.
5353

5454
Example:
5555

@@ -65,17 +65,38 @@ dump(testInstance.myStaticFunction()); // Outputs: "mockstatic"
6565
```
6666

6767
### Why This Matters for Testing
68-
- Static functions can be dynamically **mocked per instance** without modifying the component.
68+
- There is **no need** to create instance wrapper functions for static functions.
69+
- Static functions can be dynamically **mocked per instance** without modifying the class.
6970
- This allows for **cleaner test code** and avoids unnecessary duplication.
7071

72+
## Retrieving Static and Instance Function Metadata
73+
74+
Lucee provides a way to retrieve metadata for both static and instance functions using `getMetadata`. The structure of the returned metadata is identical for both, with the only difference being a `static` flag:
75+
76+
```
77+
testInstance = new Test();
78+
dump(getMetadata(testInstance).functions);
79+
```
80+
81+
Example output:
82+
83+
```
84+
[
85+
{"name": "myInstanceFunction", "static": false,...},
86+
{"name": "myStaticFunction", "static": true,...}
87+
]
88+
```
89+
90+
This means that **static and instance functions are represented the same way in metadata**, with the `static` flag indicating whether a function is static or not.
91+
7192
## The Benefit of Static Functions
7293

7394
1. **No Difference in Access** – Static functions work exactly like instance functions.
7495
2. **Ease of Mocking** – Static functions can be mocked at the instance level, avoiding unnecessary wrappers.
7596
3. **Consistency** – Static functions ensure a uniform implementation across instances while still allowing for instance-level customization when needed.
76-
4. **Overlay vs. Overwrite** – When an instance function is redefined (mocked), it overwrites the original implementation for that instance. With static methods, defining an instance-level function of the same name overlays the static method for that instance only, while the original static method remains accessible via the class.
97+
4. **Overlay vs. Overwrite** – When an instance function is redefined (mocked), it **overwrites** the original implementation for that instance. With static functions, defining an instance-level function of the same name **overlays** the static function for that instance only, while the original static function remains accessible via the class.
7798

7899
## Conclusion
79100

80-
Lucee’s handling of static functions provides a powerful way to structure shared functionality while maintaining flexibility in testing. Instead of introducing redundant instance function wrappers, developers can take advantage of the fact that static functions are accessible like instance functions and can be dynamically mocked per instance. This results in a cleaner, more maintainable codebase.
101+
Lucee’s handling of static functions provides a powerful way to structure shared functionality while maintaining flexibility in testing. Instead of introducing redundant instance function wrappers, developers can take advantage of the fact that static functions are accessible like instance functions and can be dynamically mocked per instance. Additionally, `getMetadata` provides an easy way to differentiate between static and instance functions, reinforcing the consistent handling of both in Lucee.
81102

0 commit comments

Comments
 (0)