0

I am writing a sample unit test that checks a title of a post for a token and if it exists add the post to a category.

https://gist.github.com/matstars/bebedbacedd7a85d8dc7

In my test for this I am running into something very strange. I have a stubbed method (Demo_Plugin::get_listing) in my test Test_Demo_Plugin::test_categorize_post and I use the ReflectionClass to set a private property (listing) that would have been set in the Demo_Plugin::get_listing -- but that doesn't run because I set it as a stubbed method...

I have a dataProvider that feeds a bunch of test data into the Test_Demo_Plugin::test_categorize_post method and the first set of data works as expected. It feeds in the data and sets the Demo_Plugin::listing as expected. After that it stops stubbing the Demo_Plugin::get_listing method and actually hits the method. I can't seem to figure out why a stubbed method would get called.

I even tried setting the Demo_Plugin::listing to public and setting the property in the test class after creating the object and I have the same results. The first run through the dataProvider works as expected, then it just stops stubbing Demo_Plugin::get_listing and calls the actual method. How can I get it to continue to use the stub?

Thanks for any help!

2
  • I'm not sure why that happens, but some ideas to maybe try: Pull out "->expects( $this->once() )" for now (leaving the method and return calls) - maybe PHPUnit thinks this means it should only return that value once and is maybe re-using the old mock somehow, though unlikely. – Bryan Petty Sep 18 '14 at 19:36
  • Thanks Bryan -- that didn't work as expected. This is so weird. – setterGetter Sep 18 '14 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.