You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think it's quite common to have some WP methods that require an explicit require_once (because they are not loaded by default, like in this case).
At the moment I am extracting that piece of code into a separate function (under my namespace) and then I am just using this method from my original code and patching it in the test. But I was wondering if there is any better alternative?
Writing a wrapper for functions to mock is not an uncommon practice, especially for projects like WordPress code that do not implement autoloading for objects and make large use of functions (not auto-loadable).
It works because when you let Brain Monkey mock the function, it'll declare the function for you, so the condition will always be false in your tests, even assuming you define ABSPATH there.
Sure, function_exists adds some overhead to the execution, but considering I'm not expecting you're calling this code on every request and surely (I hope) in nothing "performance critical", then you might be okay in this teeny tiny overhead.
Please note: I'm not saying this is better or worse approach, it's just another approach.
Hey,
I have been using BrainMonkey recently (thanks for it!).
I have the following piece of code at the end of a method I want to cover with unit tests:
In the test I am adding this expectation:
I think it's quite common to have some WP methods that require an explicit
require_once
(because they are not loaded by default, like in this case).At the moment I am extracting that piece of code into a separate function (under my namespace) and then I am just using this method from my original code and patching it in the test. But I was wondering if there is any better alternative?
This is my current approach:
functions.php
code.php
test.php
Functions\expect( 'Aaron\dbDelta )->once();
The text was updated successfully, but these errors were encountered: