Skip to content

Commit

Permalink
Add small test for service builder
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski committed May 14, 2024
1 parent 914dd89 commit fe692cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Test that test groups are assigned correctly using:
# `cargo nextest show-config test-groups`
[test-groups]
# Mocks of static methods need to run sequentially. This test group is for tests that mock AppService's static methods.
app-service-static-mock = { max-threads = 1 }

[[profile.default.overrides]]
filter = 'test(service::registry::tests::)'
filter = 'test(#service::registry::tests::*) | test(#service::tests::*)'
test-group = "app-service-static-mock"
38 changes: 38 additions & 0 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,41 @@ where

async fn build(self, context: &AppContext<A::State>) -> anyhow::Result<S>;
}

#[cfg(test)]
mod tests {
use crate::app::MockTestApp;
use crate::app_context::MockAppContext;
use crate::service::{AppServiceBuilder, MockAppService};
use async_trait::async_trait;
use rstest::rstest;

struct TestAppServiceBuilder;
#[async_trait]
impl AppServiceBuilder<MockTestApp, MockAppService<MockTestApp>> for TestAppServiceBuilder {
async fn build(
self,
_context: &MockAppContext<()>,
) -> anyhow::Result<MockAppService<MockTestApp>> {
Ok(MockAppService::default())
}
}

#[rstest]
#[case(true)]
#[case(false)]
fn builder_enabled(#[case] service_enabled: bool) {
// Arrange
let mut context = MockAppContext::default();
context.expect_clone().returning(MockAppContext::default);

let enabled_ctx = MockAppService::<MockTestApp>::enabled_context();
enabled_ctx.expect().returning(move |_| service_enabled);

// Act
let builder = TestAppServiceBuilder;

// Assert
assert_eq!(builder.enabled(&context), service_enabled);
}
}

0 comments on commit fe692cb

Please sign in to comment.