From 8c6431fd834337be7f6b88e3c8a29c6a17e0b9cf Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 24 Oct 2022 22:32:42 +0200 Subject: test: add binding configurator unit tests --- .../blocking/binding/scope_configurator.rs | 50 ++++++++++++++++++++++ .../blocking/binding/when_configurator.rs | 38 ++++++++++++++++ 2 files changed, 88 insertions(+) (limited to 'src/di_container/blocking') diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index 5553415..dc33cbc 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -79,3 +79,53 @@ where Ok(BindingWhenConfigurator::new(self.di_container.clone())) } } + +#[cfg(test)] +mod tests +{ + use super::*; + use crate::test_utils::{mocks, subjects}; + + #[test] + fn in_transient_scope_works() + { + let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new(); + + di_container_mock + .expect_set_binding::() + .withf(|name, _provider| name.is_none()) + .return_once(|_name, _provider| ()) + .once(); + + let binding_scope_configurator = BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock)); + + binding_scope_configurator.in_transient_scope(); + } + + #[test] + fn in_singleton_scope_works() + { + let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new(); + + di_container_mock + .expect_set_binding::() + .withf(|name, _provider| name.is_none()) + .return_once(|_name, _provider| ()) + .once(); + + let binding_scope_configurator = BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock)); + + assert!(matches!( + binding_scope_configurator.in_singleton_scope(), + Ok(_) + )); + } +} diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index 5b9a8c0..49c9d9e 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -61,3 +61,41 @@ where Ok(()) } } + +#[cfg(test)] +mod tests +{ + use mockall::predicate::eq; + + use super::*; + use crate::provider::blocking::MockIProvider; + use crate::test_utils::{mocks, subjects}; + + #[test] + fn when_named_works() + { + let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new(); + + di_container_mock + .expect_remove_binding::() + .with(eq(None)) + .return_once(|_name| Some(Box::new(MockIProvider::new()))) + .once(); + + di_container_mock + .expect_set_binding::() + .withf(|name, _provider| name == &Some("cool")) + .return_once(|_name, _provider| ()) + .once(); + + let binding_when_configurator = BindingWhenConfigurator::< + dyn subjects::INumber, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock)); + + assert!(matches!( + binding_when_configurator.when_named("cool"), + Ok(_) + )); + } +} -- cgit v1.2.3-18-g5258