Now we have the repository in place from the last lesson, let's move on to testing that the posts are returned in the correct order.
We want them to be returned based on their published date and not by their node ID or anything else.
To do this, we will use a different type of test - a Kernel test.
## Introducing Kernel tests
So far, we've been using Functional (or Browser) tests to ensure the blog page exists and that the correct posts are displayed.
It's easy to assert the correct posts are shown on the page, but it's much harder to assert they're shown in the right order.
This is much easier to do with a Kernel test (aka. an integration test).
Instead of making HTTP requests and checking the responses, we can test the results from the repository and ensure it returns the results in the correct order.
## Writing your first Kernel test
Let's create a new test that uses the `PostNodeRepository` to find the nodes and assert we get an expected number returned.
As with the Functional test, the file and class name must have a `Test` suffix, and test methods should have a `test` prefix. As we're testing the `PostNodeRepository` class, the convention is to name the test `PostNodeRepositoryTest`.
As this is a Kernel test, it should be placed within the `tests/src/Kernel` directory and extend the `EntityKernelTestBase` class.
We could extend others, such as the regular `KernelTestBase`, but as we'll be working with nodes, `EntityKernelTestBase` is the better option.
Instead of making assertions based on the HTTP response, we're testing what's returned from the `findAll()` method.
### Resolving setup test failures
Run the tests to see the first error:
> Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "Drupal\atdc\Repository\PostNodeRepository".
Although you created the `PostNodeRepository` in the previous lesson and added it as a service, it's not found.
It's defined within `atdc.services.yml`, but we need to be explicit about which modules are enabled when running the test, and the `atdc` module isn't enabled.
Create a `$modules` array within the test class and add `atdc`:
Next, let's assert they're returned in a specific order.
Update the posts to have a specific title and created date so we can specify which order we expect them to be returned in and which titles they should have:
'created' => (new DrupalDateTime('-1 week'))->getTimestamp(),
'title' => 'Post one',
'type' => 'post',
]);
$this->createNode([
'created' => (new DrupalDateTime('-8 days'))->getTimestamp(),
'title' => 'Post two',
'type' => 'post',
]);
$this->createNode([
'created' => (new DrupalDateTime('yesterday'))->getTimestamp(),
'title' => 'Post three',
'type' => 'post',
]);
```
Note we're intentionally setting them to be in an incorrect order, to begin with, so the test doesn't pass accidentally. This way, we can see it fail and know the task is complete once it passes.
Next, assert that the titles are returned in the correct order.