Testing
Testing peer-to-peer systems can be challenging, but worth the initial effort to ensure your data model works across multiple instances. It is important to test your business logic with end-to-end unit tests that involve multiple Ditto peers.
A typical test lifecycle is:
- Create two instances of Ditto with the same identity and different directories.
- Call startSync() on both instances.
- Listen for changes to data using subscribe() and write tests as a response to the observeLocal() function.
- Clean up Ditto directories between tests and stop any open synchronization threads using stopSync().
For complete examples and running tests, see the open source samples GitHub repository.
Getting Started
To test Ditto using SwiftUI, we provide you with some basic convenience functions you can use to get started.
For code that you can use in your project, see the example on GitHub.
Create custom directories
Each instance of Ditto should use a different directory. You can create a convenience class to provide a custom directory for every instance of Ditto.
Create your test
- Initialize two ditto instances with different directories.
- Call startSync() on both instances.
Listen for Data Changes
Using observeLocal(), we listen to changes to data. We write tests as a response to the observeLocal() function based on what we expect to happen.
The tests should reside within the callback so that they properly test the state of the database after synchronization is complete.
Clean up
Don't forget to clean up between tests. This can also be implemented as part of a base test class.
Full example
For the full example that you can use in your project, see the open source testing library on GitHub.`