Skip to content
Discussion options

You must be logged in to vote

Your D should 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 MyObject for tests might look something like this:

struct MyServices<R: Repository> {
  repository: R,
}

trait Repository {
  get_my_object(&self, id: &str) -> Result<MyObject,Error>;
  add_or_update_my_object(&self, id: &str, my_object: MyObject) -> Result<(),Error>;
}


// Real repository for use in live systems
struct PostgresRepository {
  // your database connection
   conn: DatabaseConnection,
}

impl Repository for PostgresRepository {
...
}


// A mock repository using a hashmap to simulate a database.
struct RepositoryStub {
   test_data:…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@turtletongue
Comment options

Answer selected by turtletongue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants