Expand description
§Mock credential store
To facilitate testing of clients, this crate provides a Mock credential store that is platform-independent, provides no persistence, and allows the client to specify the return values (including errors) for each call. The credentials in this store have no attributes at all.
To use this credential store instead of the default, make this call during application startup before creating any entries:
keyring_core::set_default_store(keyring_core::mock::Store::new().unwrap());You can then create entries as usual and call their usual methods to set, get, and delete passwords. There is no persistence except in-memory so, once you drop the store, all the credentials will be gone.
If you want a method call on an entry to fail in a specific way, you can downcast the entry to a Cred and then call set_error with the appropriate error. The next entry method called on the credential will fail with the error you set. The error will then be cleared, so the next call on the mock will operate as usual. Setting an error will not affect the value of the credential (if any). Here’s a complete example:
keyring_core::set_default_store(mock::Store::new().unwrap());
let entry = Entry::new("service", "user").unwrap();
entry.set_password("test").expect("the entry's password is now test");
let mock: &mock::Cred = entry.as_any().downcast_ref().unwrap();
mock.set_error(Error::Invalid("mock error".to_string(), "takes precedence".to_string()));
_ = entry.get_password().expect_err("the error will be returned");
let val = entry.get_password().expect("the error has been cleared");
assert_eq!(val, "test", "the error did not affect the password");