Skip to content

Best Practices

Write focused assertions

Instead of creating overly complex assertion that check everything at once create multiple assertions focused on specific aspects. As general guide start by asserting the basics once and then assume them to be true in more detailed assertions. This makes assertions simpler and more understandable. If an assertion fails you know directly why and don't have to analyse which aspect broke it.

Directly compare foreign keys if entity is not asserted

When an entity is referenced it is included in the generated SQL queries as at least one join. If you only care that two entities reference the same entity and do not assert the entity itself as in

postA.Author.IsEqualTo(postB.Author)
then you can directly compare the foreign keys as in
postA.AuthorId.IsEqualTo(postB.AuthorId)

Try different versions

Most assertions can be written in multiple ways. Most times there is one way that reads and feels most natural, but it is up to you to find that version. Especially trying both For(All, ...) and For(None, ...) as the entrypoint can change things up.

Write helper methods

When you find yourself writing a certain combination of constraints over and over for an entity put it into a helper method on the entity class. You don't repeat yourself and if you need to ever change it you can do so in a single place.