-
Notifications
You must be signed in to change notification settings - Fork 29
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
Expectation on "times" required for withArgs
#52
Comments
Instead of this: Brain\Monkey\Filters\expectAdded('template_include')
->with('Example->template()'); try this: static::assertTrue( has_filter( 'template_include', 'Example->template()' ) ); (Or This needs to go after you instantiated the object, though. |
Yes that works, but shouldn't the expectation work as well? It seems to be the preferred way to test actions/filters. |
I believe Is this assumption corect @gmazzap? And if so, has this been done on purpose? |
I saw a version from a blog article that used expectAdded('filter')->with(function ($cb) {
return $cb[0] instanceof Example && $cb[1] === 'template';
}); But that also gave me the same result. |
@coreyworrell, as @tfrommen said, the "string representation" of a callback only works with core functions: When using Mockery methods you have to stick with what Mockery offers. The latest snippet you posted: expectAdded('filter')->with(function ($cb) {
return $cb[0] instanceof Example && $cb[1] === 'template';
}); Will not work because that is telling Mockery to expect the closure as the only argument, which can never be true, because the closure is instantiated right in inside the test. The correct way should be: expectAdded('filter')->withArgs(function ($cb) {
return $cb[0] instanceof Example && $cb[1] === 'template';
}); In fact, Right now, I can't test if in Brain Monkey that works as expected, if you test it please let us know. Thanks! |
Thanks @gmazzap! That's what I was missing, difference between |
@gmazzap Just tested this again, it does not seem to work. The test passes no matter what. Even this passes: expectAdded('wp_dashboard_setup')->withArgs(function () {
return false;
}); |
Thanks @coreyworrell I found the issue. When using For example: expectAdded('template_include')
->once() // <~ this
->withArgs(function() { return false; }); or even: expectAdded('template_include')
->atLeast()->once() // <~ this
->withArgs(function() { return false; }); When using At the moment I've no time to dig into the reasons of this unconsistence, but I'll try to see if it is something that can be fixed in Brain Monkey or depends on Mockery. If not fixable in Brain Monkey, it should at least be documented. Thanks for reporting. |
withArgs
Thanks for looking into this and for the fix! |
This |
What am I doing wrong here?
Returns
The text was updated successfully, but these errors were encountered: