Using factory.php outside of trunk for unit testing

The problem is not that the factory class is unavailable, because if that were so you would be getting a fatal “class not found” error. The problem is that you aren’t using your test case properly. This code (and similar) is what is causing the problem: /** * Testing that timeline_express_get_announcement_date() returns what we expect … Read more

How do I mock get_adjacent_post for testing

I used WP_Mock for a long time, until I built Brain Monkey to overcome some problems I found working with it. Using Brain Monkey you can: use Brain\Monkey\Functions; Functions::when(‘get_adjacent_post’)->alias(function() { // mock here… }); or Functions::expect(‘get_adjacent_post’) ->atLeast() ->once() ->with( true, ”, true, ‘topic’ ) ->andReturnUsing(function( $in_same_term, $excluded_terms, $previous ) { // mock here }); This … Read more

How to set up a user inside unit tests

Just few notes: Looks like you’re doing functional/integration tests rather than unit tests in isolation. Here you’re creating a user, but not setting it as the current one: $user = $this->factory->user->create(); wp_set_current_user( 1 ); You probably want: $user_id = $this->factory->user->create(); wp_set_current_user( $user_id ); Note that cookies aren’t set here, because of: tests_add_filter( ‘send_auth_cookies’, ‘__return_false’ ); … Read more