1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Mock example
//!
//! Mocking an actor and setting it on the `SystemRegistry`. This can be
//! done so you can test an actor that depends on a `SystemService` in isolation.
#[cfg(test)]
use actix::actors::mocker::Mocker;
use actix::prelude::*;
#[cfg(test)]
use actix::SystemRegistry;

#[derive(Default)]
struct MyActor {}

impl Actor for MyActor {
    type Context = Context<Self>;
}

impl Handler<Question> for MyActor {
    type Result = ResponseFuture<usize>;

    fn handle(&mut self, _msg: Question, _ctx: &mut Context<Self>) -> Self::Result {
        let act_addr = AnswerActor::from_registry();
        let request = act_addr.send(Question {});
        Box::pin(async move { request.await.unwrap() })
    }
}

#[derive(Default)]
struct AnsActor {}

#[derive(Message)]
#[rtype(usize)]
struct Question {}

#[cfg(not(test))]
type AnswerActor = AnsActor;

#[cfg(test)]
type AnswerActor = Mocker<AnsActor>;

impl Actor for AnsActor {
    type Context = Context<Self>;
}

impl Supervised for AnsActor {}

impl SystemService for AnsActor {}

impl Handler<Question> for AnsActor {
    type Result = usize;

    fn handle(&mut self, _: Question, _: &mut Context<Self>) -> Self::Result {
        42
    }
}

#[actix::main]
async fn main() {
    let addr = MyActor::default().start();
    let result = addr.send(Question {}).await.unwrap_or_default();
    assert_eq!(42, result);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[actix::test]
    async fn mocked_behavior() {
        let mocker_addr = AnswerActor::mock(Box::new(move |_msg, _ctx| {
            let result: usize = 2;
            Box::new(Some(result))
        }))
        .start();
        SystemRegistry::set(mocker_addr);
        let result = MyActor::default().start().send(Question {}).await.unwrap();
        assert_eq!(result, 2);
    }
}