Skip to main content

openlark_core/testing/
fixtures.rs

1//! 统一测试夹具系统
2//!
3//! 提供测试配置的快速构建,避免在每个测试中重复设置。
4
5use crate::config::Config;
6
7/// 测试配置构建器
8///
9/// 提供合理的默认值,快速创建测试用的配置。
10///
11/// # 示例
12///
13/// ```rust,ignore
14/// let config = TestConfigBuilder::new()
15///     .app_id("test_app")
16///     .app_secret("test_secret")
17///     .build();
18/// ```
19#[derive(Debug, Clone)]
20pub struct TestConfigBuilder {
21    app_id: String,
22    app_secret: String,
23    base_url: String,
24}
25
26impl TestConfigBuilder {
27    /// 创建新的测试配置构建器(使用默认值)
28    pub fn new() -> Self {
29        Self {
30            app_id: "test_app_id".to_string(),
31            app_secret: "test_app_secret".to_string(),
32            base_url: "https://open.feishu.cn".to_string(),
33        }
34    }
35
36    /// 设置应用 ID
37    pub fn app_id(mut self, id: impl Into<String>) -> Self {
38        self.app_id = id.into();
39        self
40    }
41
42    /// 设置应用密钥
43    pub fn app_secret(mut self, secret: impl Into<String>) -> Self {
44        self.app_secret = secret.into();
45        self
46    }
47
48    /// 设置基础 URL
49    pub fn base_url(mut self, url: impl Into<String>) -> Self {
50        self.base_url = url.into();
51        self
52    }
53
54    /// 构建配置实例
55    pub fn build(self) -> Config {
56        Config::builder()
57            .app_id(self.app_id)
58            .app_secret(self.app_secret)
59            .base_url(self.base_url)
60            .build()
61    }
62}
63
64impl Default for TestConfigBuilder {
65    fn default() -> Self {
66        Self::new()
67    }
68}
69
70/// 快捷方法:创建默认测试配置
71pub fn test_config() -> Config {
72    TestConfigBuilder::new().build()
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_test_config_builder() {
81        let config = TestConfigBuilder::new()
82            .app_id("my_app")
83            .app_secret("my_secret")
84            .build();
85
86        assert_eq!(config.app_id(), "my_app");
87        assert_eq!(config.app_secret(), "my_secret");
88    }
89
90    #[test]
91    fn test_test_config_default() {
92        let config = test_config();
93        assert_eq!(config.app_id(), "test_app_id");
94        assert_eq!(config.app_secret(), "test_app_secret");
95    }
96}