pub struct MockPublisher<E> {
published: Arc<Mutex<Vec<E>>>,
}Expand description
In-memory Publisher for use in unit tests.
Captures all published events so tests can assert on what was published without needing a running NATS server.
MockPublisher<E> can only capture events of exactly type E. To capture
multiple event types in one publisher, wrap them in an enum or use
MockBus.
§Example
use arminius_lib_message_bus::{Event, Publisher, mock::MockPublisher};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct RoomCreated {
room_id: String,
}
impl Event for RoomCreated {
const SUBJECT: &str = "RoomCreated.v1";
}
let publisher: MockPublisher<RoomCreated> = MockPublisher::new();
publisher.publish(RoomCreated { room_id: "!abc".into() }).await.unwrap();
assert_eq!(publisher.published_messages().len(), 1);Fields§
§published: Arc<Mutex<Vec<E>>>All events published so far, in order.
Implementations§
Source§impl<E: Clone> MockPublisher<E>
impl<E: Clone> MockPublisher<E>
Sourcepub fn published_messages(&self) -> Vec<E>
pub fn published_messages(&self) -> Vec<E>
Returns a snapshot of all events published so far, in order.
Trait Implementations§
Source§impl<E: Clone> Clone for MockPublisher<E>
impl<E: Clone> Clone for MockPublisher<E>
Source§fn clone(&self) -> MockPublisher<E>
fn clone(&self) -> MockPublisher<E>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<E: Debug> Debug for MockPublisher<E>
impl<E: Debug> Debug for MockPublisher<E>
Source§impl<E: Default> Default for MockPublisher<E>
impl<E: Default> Default for MockPublisher<E>
Source§fn default() -> MockPublisher<E>
fn default() -> MockPublisher<E>
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl<E> Freeze for MockPublisher<E>
impl<E> RefUnwindSafe for MockPublisher<E>
impl<E> Send for MockPublisher<E>where
E: Send,
impl<E> Sync for MockPublisher<E>where
E: Send,
impl<E> Unpin for MockPublisher<E>
impl<E> UnsafeUnpin for MockPublisher<E>
impl<E> UnwindSafe for MockPublisher<E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more