Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eisberg-labs/actix-actor-expect
Utility for unit testing actix actors, extension for `Mocker`
https://github.com/eisberg-labs/actix-actor-expect
actix actor unit-testing
Last synced: 6 days ago
JSON representation
Utility for unit testing actix actors, extension for `Mocker`
- Host: GitHub
- URL: https://github.com/eisberg-labs/actix-actor-expect
- Owner: eisberg-labs
- License: apache-2.0
- Created: 2021-06-28T06:39:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-20T20:51:19.000Z (over 1 year ago)
- Last Synced: 2024-10-31T11:53:06.216Z (16 days ago)
- Topics: actix, actor, unit-testing
- Language: Rust
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE-APACHE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Actix Actor Expect [![Continuous Integration](https://github.com/eisberg-labs/actix-actor-expect/actions/workflows/ci.yml/badge.svg)](https://github.com/eisberg-labs/actix-actor-expect/actions/workflows/ci.yml) [![cargo-badge][]][cargo] [![license-badge][]][license]
Utility for unit testing actix actors, extension for `Mocker`. I wrote a blog post [Mocking Actix Actor without getting a gray hair](https://amarjanica.com/mocking-actix-actor-without-getting-a-gray-hair/) a
while ago, you might find it useful.# Usage
Dependencies:```toml
[dev-dependencies]
actix-actor-expect = "0.1.0"
```Code:
```rust#[derive(Clone, Debug, PartialEq, Message)]
#[rtype(result = "Result")]
pub enum TestActorCommand {
Hello,
Dunno,
Echo(String),
}#[derive(Debug, Default)]
pub struct TestActor {}impl Actor for TestActor {
type Context = Context;
}impl Handler for TestActor {
type Result = Result;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::::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](./tests).
# License
Distributed under the terms of [MIT license](./LICENSE-MIT) and [Apache license](./LICENSE-APACHE).
[cargo-badge]: https://img.shields.io/crates/v/actix-actor-expect.svg?style=flat-square
[cargo]: https://crates.io/crates/actix-actor-expect
[license-badge]: https://img.shields.io/badge/license-MIT/Apache--2.0-lightgray.svg?style=flat-square
[license]: #license