Test custom WordPress rest API endpoint units

You can extend and change the named_route attribute value to easily adjust it to work with multiple paths. This test will check whether the paths are added correctly. This is especially helpful in preventing future changes from accidentally deleting paths. Path response test another thing I said we should do is test whether the API can generate the correct response. When I first thought of creating this type of test, I thought I should create an HTTP request for the path and then test the response. But it’s too complicated. The
Instead, assume that the WordPress rest API infrastructure works, creating a simulation request and validating the response. This is how testing a basic endpoint works. Reading their sources is the way I found it. For example, copy the test from the test of the default back endpoint. Male factory->post->create (array (‘post\u author’= > $this->editor\u ID));
$this->factory->post->create (array (‘post\u author’= > $this->author\u ID));
\/\/3 posts in total
$request= new wp_rest_request (\
$response = $this->server->dispatch ($request);
$this->assertequals (200, $response->get\u status());
$this->assertequals (3, count ($response->get\u data()));
\/\/Two of the three posts
$request= new wp_rest_request (\
$request->set\u param (\
$response = $this->server->dispatch ($request);
$this->assertequals (200, $response->get\u status());
$data = $response->get\u data();
$this->assertequals (2, count ($data));
$this->assertequalsets (array ($this->editor\u ID, $this->author\u ID), wp\u list\u plug ($data,’author’));
\/\/One of three posts
$request= new wp_rest_request (\
$request->set\u param (\
$response = $this->server->dispatch ($request);
$this->assertequals (200, $response->get\u status());
$data = $response->get\u data();
$this->assertequals (1, count ($data));
$this->assertequals ($this->editor_id, $data[0][‘author’]);
}This code involves various scenario tests. Every time a request is generated with a new instance of the wp\u rest\u request() class, and the wp\u rest\u server is used to dispatch it and generate a confirmatable response. This is a good example. You can get posts through the rest API response format without sending back the request. The
Let’s use it to test the path. Assume that there is a path at the end of the URL that uses the ID and returns the project name. We would like to conduct the following tests: Class test_api extension wp\u unittestcase{

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *