Tutorials Clean Architecture & DDD Mastery
Unit Testing the Domain: Fast and pure
On this page
Pure Logic Testing
Because your Domain layer has zero dependencies, it is the easiest and fastest part of your application to test.
1. Testing Business Rules
You don't need mocks to test a Domain Entity. You just instantiate the class and call its methods. var user = new User(); user.Deactivate(); Assert.False(user.IsActive);. These tests run in milliseconds. You can run 1,000 of them in seconds, giving you near-instant feedback on your business rules.
2. Behavioral Testing
Focus on 'Behavior' rather than 'State'. Test that an entity raises the correct **Domain Event** when a certain condition is met. This ensures that the 'intent' of your domain logic is captured and preserved as the code evolves.
3. Architect Insight
Q: "Should I test my private fields?"
Architect Answer: "NO. Test the public API of your domain. If a business rule requires that a Discount can't be negative, test that passing a negative value to the ApplyDiscount() method throws an error (or returns a failure result). Testing internals makes your tests brittle. Testing behavior makes your architecture robust."