5个版本
0.1.0 | 2022年3月7日 |
---|---|
0.1.0-beta.2 | 2022年3月4日 |
在 测试 中排名 599
9KB
97 行
Actix Actor Expect

用于单元测试actix演员的实用工具,Mocker的扩展。我之前写了一篇关于如何“Mock Actix Actor而不变灰白头发的博客” Mocking Actix Actor without getting a gray hair,可能对你有用。
用法
依赖
[dev-dependencies]
actix-actor-expect = "0.1.0"
代码
#[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
);
}
还可以查看测试。
许可证
依赖
~4–13MB
~141K SLoC