English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet test --solution Chatex.slnxpowershell
dotnet format Chatex.slnx --verify-no-changesThe test projects must be identical to the reference:
powershell
git diff --no-index Tests/Chatex.Testing D:\Work\Chatex-Reference\Tests\Chatex.Testingpowershell
git diff --no-index Tests/Chatex.Data.Tests D:\Work\Chatex-Reference\Tests\Chatex.Data.TestsCompare Tests/Directory.Build.props and Tests/xunit.runner.json the same way, and ignore the folders bin and obj. The whole folder Tests cannot be compared at once: the reference also contains Chatex.Core.Tests and Chatex.Api.Tests from later topics.
Commit:
powershell
git add .powershell
git commit -m "Test the data layer against PostgreSQL in a container"The data layer is finished: module 2 is complete.
Common mistakes
Table 6.1. Common mistakes in the data tests
| Symptom | Cause | Fix |
|---|---|---|
Docker is either not running or misconfigured | Docker Desktop is not running | start it and wait for the engine |
CS0234: The type or namespace name 'Testing' does not exist in the namespace 'Chatex' | Chatex.Testing has no types yet, or the test project has no reference to it | add PostgresFixture and DatabaseTest; check Tests/Directory.Build.props |
| test projects build without warnings-as-errors or without nullable | Tests/Directory.Build.props does not import the root file | keep the <Import Project="$([MSBuild]::GetPathOfFileAbove(...))" /> line |
The following constructor parameters did not have matching fixture data: PostgresFixture fixture | the [assembly: AssemblyFixture(typeof(PostgresFixture))] attribute is missing | add it at the top of DbTestBase.cs |
| tests fail randomly with missing or extra rows | tests run in parallel against one database | xunit.runner.json with "parallelizeTestCollections": false, copied by Tests/Directory.Build.props |
Cannot resolve scoped service 'ChatexDbContext' from root provider | a scoped service is resolved from the root provider | resolve it from NewScope() |
| an assertion sees an entity that was never saved | the same scope was used to arrange and to assert | arrange in one scope, act and assert in a new one |
no tests are found, or dotnet test complains about VSTest | global.json lacks the Microsoft Testing Platform setting | add "test": { "runner": "Microsoft.Testing.Platform" } (topic 2) |
Self-check questions
- Why do the data tests run against PostgreSQL instead of EF Core's in-memory provider or SQLite? Name three features of the data layer that only a real PostgreSQL can test.
- What does Testcontainers do, and why does every test project get its own container on a random port?
- Why are the tests independent although they share one database? What does Respawn do, and what does it leave alone?
- Why does the fixture apply the real migrations instead of
EnsureCreated? - What is an assembly fixture in xUnit v3? How does a test class receive it?
- Why does
Tests/Directory.Build.propsimport the rootDirectory.Build.props? What decides whether a project gets the test settings? - Why are parallel collections turned off? Which tests still run in parallel?
- Why do the tests arrange data in one scope and assert in another?
- What are the three parts of a test? Why should a test be seen failing at least once?
- Why does
AddMessagesAsynccreate ids withGuid.CreateVersion7(timestamp)instead ofGuid.CreateVersion7()? - Which conditions does the chat list test cover with its four chats?
- How does the rollback test prove that a transaction works?
Exercises
- A missing test.
IChatRepository.TouchAsyncmust never moveLastActivityAtbackwards. WriteTouchAsync_only_moves_the_activity_forwardinChatRepositoryTests: create a chat with a known activity time, callTouchAsyncwith an earlier and a later time, and check the stored value after each call in a new scope. - Stale attachments. Write a test for
IAttachmentRepository.GetStalePendingAsync: three uploads without a message at differentCreatedAttimes (set them explicitly — the interceptor keeps a time that is notdefault), one upload linked to a message; ask for uploads older than a cut-off with a limit of 1 and check that the oldest unlinked upload comes back. - A theory. Turn
Only_owners_and_admins_post_in_channelsinto a[Theory]with[InlineData(ChatMemberRole.Owner, ChatType.Channel, true)]and so on for all twelve combinations of role and chat type. Which combinations make no sense in practice, and should the test include them? - Break the cursor. In
Paging.ToPage, return the cursor ofitems[^2]instead ofitems[^1]after removing the extra item. Which tests fail, and what do their messages tell you?