Crates.io | actix-actor-expect |
lib.rs | actix-actor-expect |
version | 0.1.0 |
source | src |
created_at | 2022-03-04 15:05:16.380889 |
updated_at | 2022-03-07 11:48:03.738933 |
description | Utility for unit testing actix actors |
homepage | |
repository | https://github.com/eisberg-labs/actix-actor-expect |
max_upload_size | |
id | 543557 |
size | 21,061 |
Utility for unit testing actix actors, extension for Mocker
. I wrote a blog post Mocking Actix Actor without getting a gray hair a
while ago, you might find it useful.
Dependencies:
[dev-dependencies]
actix-actor-expect = "0.1.0"
Code:
#[derive(Clone, Debug, PartialEq, Message)]
#[rtype(result = "Result<String, Error>")]
pub enum TestActorCommand {
Hello,
Dunno,
Echo(String),
}
#[derive(Debug, Default)]
pub struct TestActor {}
impl Actor for TestActor {
type Context = Context<Self>;
}
impl Handler<TestActorCommand> for TestActor {
type Result = Result<String, Error>;
fn handle(&mut self, msg: TestActorCommand, _ctx: &mut Self::Context) -> Self::Result {
match msg {
TestActorCommand::Echo(message) => Ok(message),
rest => Ok(format!("{:?}", rest)),
}
}
}
#[actix::test]
async fn sends_hello_back() {
let actor_expect = ActorExpect::<TestActor, Error>::expect_send(
TestActorCommand::Echo("Message".to_string()),
"Message".to_string(),
None,
);
let actor = &actor_expect.addr;
let _ = actor
.send(TestActorCommand::Echo("Message".to_string()))
.await
.expect("Not able to process Echo");
let _ = actor
.send(TestActorCommand::Hello)
.await
.expect("Not able to process Hello");
let _ = actor
.send(TestActorCommand::Dunno)
.await
.expect("Not able to process Dunno");
assert_eq!(actor_expect.total_calls(), 3_usize);
assert_eq!(
actor_expect.calls_of_variant(TestActorCommand::Echo("Message".to_string())),
1_usize
);
}
Also take a look at tests.
Distributed under the terms of MIT license and Apache license.