SDK Guides
...
Swift
Quick Tips for Swift

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:

  1. Create two instances of Ditto with the same identity and different directories.
  2. Call startSync() on both instances.
  3. Listen for changes to data using subscribe() and write tests as a response to the observeLocal() function.
  4. 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.

Swift


Create your test

  1. Initialize two ditto instances with different directories.
  2. Call startSync() on both instances.
Swift


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.

Swift


Clean up

Don't forget to clean up between tests. This can also be implemented as part of a base test class.

Swift


Full example

For the full example that you can use in your project, see the open source testing library on GitHub.`