Unit testing aggregates with services #233
|
I'm exploring this very helpful crate but got stuck with a testing issue of aggregates that have services accessing database. Initially I planned to make services generic over database client to swap it in unit tests but generic arguments can't be passed to Services in Aggregate trait implementation because they will be unconstrained (E0207). Only way to resolve the issue without changing the trait suggested by rust book is to add PhantomData to aggregate holding the generic argument. But I don't think it's good idea to add database client to my domain model even as PhantomData. Or maybe I'm wrong? So, is it true the only way to swap services implementation in unit tests is via defining separate services using cfg(test) attribute? |
Replies: 3 comments 1 reply
|
Hi @turtletongue, You should be able to swap out the services that you use in testing to fit whatever need that you have. Can you add an example where you are having an issue doing so? |
|
I've tried this: /// Services with dependencies to be swapped.
struct Services<D> {
/// Generic database client.
db: D,
}
/// Some domain model.
struct Account {
// ...
}
// Error here: the type parameter D is not constrained by the impl trait, self type, or predicates (E0207)
impl<D> Aggregate for Account {
// ...
type Services = Services<D>;
// ....
}Apparently, |
|
Your e.g., using a hashmap instead of a live database to store some You would then swap out the Aggregate definition in a live environment: Aggregate definition in a test: Does that answer your question? |
Your
Dshould be a trait and you'll need a test object for when you're testing.e.g., using a hashmap instead of a live database to store some
MyObjectfor tests might look something like this: