{"record":{"id":"bde15efa9574c35a","repo":"actix/actix","slug":"mocker-actor-used-before-set","errorCode":null,"errorMessage":"Mocker actor used before set","messagePattern":"Mocker actor used before set","errorType":"panic","errorClass":"panic","httpStatus":null,"severity":"critical","filePath":"actix/src/actors/mocker.rs","lineNumber":59,"sourceCode":"impl<T: Unpin> Mocker<T> {\n    #[allow(clippy::type_complexity)]\n    pub fn mock(\n        mock: Box<dyn FnMut(Box<dyn Any>, &mut Context<Mocker<T>>) -> Box<dyn Any>>,\n    ) -> Mocker<T> {\n        Mocker::<T> {\n            phantom: PhantomData,\n            mock,\n        }\n    }\n}\n\nimpl<T: SystemService> SystemService for Mocker<T> {}\nimpl<T: ArbiterService> ArbiterService for Mocker<T> {}\nimpl<T: Unpin> Supervised for Mocker<T> {}\n\nimpl<T: Unpin> Default for Mocker<T> {\n    fn default() -> Self {\n        panic!(\"Mocker actor used before set\")\n    }\n}\n\nimpl<T: Sized + Unpin + 'static> Actor for Mocker<T> {\n    type Context = Context<Self>;\n}\n\nimpl<M: 'static, T: Sized + Unpin + 'static> Handler<M> for Mocker<T>\nwhere\n    M: Message,\n    <M as Message>::Result: MessageResponse<Mocker<T>, M>,\n{\n    type Result = M::Result;\n    fn handle(&mut self, msg: M, ctx: &mut Self::Context) -> M::Result {\n        let mut ret = (self.mock)(Box::new(msg), ctx);\n        let out = ret\n            .downcast_mut::<Option<M::Result>>()\n            .expect(\"wrong return type for message\")","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/actix/actix/blob/36e5d97e41ebd16431c709365e6942db08d867d7/actix/src/actors/mocker.rs#L41-L77","documentation":"Mocker<T> is a test-double actor that wraps another actor so messages can be intercepted. It has no meaningful Default implementation: the only `default()` body is a panic, because a Mocker must be explicitly configured (via `Mocker::<T>::run(...)` or the `mock`/`set` mechanism) before the actor system tries to create one. When the system constructs a Mocker through `Default::default()` — typically via `SystemService::start_service` or `ArbiterService` registries — the panic fires, meaning the mock was never set up.","triggerScenarios":"Resolving Mocker<T> as a SystemService or ArbiterService (e.g. `Mocker::<MyActor>::from_registry()` or `start_service()`) without first calling the mock setup API (`Mocker::<MyActor>::run(...)` / providing a boxed mock handler). Any code path that requires `Default::default()` for Mocker<T>.","commonSituations":"Writing actix unit tests where the developer registers/looks up the mocker service before `run()`; copy-pasting real actor service code but swapping in Mocker without calling its setup; upgrading actix where test helpers changed order of initialization.","solutions":["Call `Mocker::<TargetActor>::run(mock_fn, |_, addr| { ... })` (or the equivalent `run_with` helper) so the mock is installed before the actor is resolved.","Do not resolve Mocker via `from_registry()`/`start_service()` expecting a default; it intentionally has no default instance.","If you did not intend a mock, use the real actor type instead of Mocker<T>.","Check test setup order: mock must be set before any code starts the service."],"exampleFix":"// before\nlet addr = Mocker::<MyActor>::from_registry();\n\n// after\nMocker::<MyActor>::run(|msg, ctx| { ...mock behavior... }, |_, addr| {\n    // use addr inside this scope\n});","handlingStrategy":"validation","validationCode":"// Ensure the mock is installed before resolving the service\nlet installed = /* your test registry flag */;\nassert!(installed, \"call Mocker::<T>::run(...) before from_registry()\");","typeGuard":null,"tryCatchPattern":"// Panics are not catchable in normal Rust code; ensure setup ordering instead.\n#[test]\n#[should_panic(expected = \"Mocker actor used before set\")]\nfn mocker_without_setup_panics() { let _ = Mocker::<MyActor>::from_registry(); }","preventionTips":["Always create Mocker via Mocker::<T>::run(...) inside the test body.","Never use Mocker as a plain default service type.","Keep mock setup in a shared test helper so ordering is guaranteed."],"tags":["actix","testing","mock","actor","panic"],"backgroundTag":"invalid-state-transition","analyzedSha":"36e5d97e41ebd16431c709365e6942db08d867d7","analyzedAt":"2026-09-11T16:51:51.370Z","contentChangedAt":"2026-09-11T16:51:51.370Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}