Loading...
Loading...
Compare original and translation side by side
MethodName_StateUnderTest_ExpectedBehaviorAdd_TwoPositiveNumbers_ReturnsCorrectSumParseDate_InvalidFormat_ThrowsFormatExceptionGetUser_UserDoesNotExist_ReturnsNullMethodName_StateUnderTest_ExpectedBehaviorAdd_TwoPositiveNumbers_ReturnsCorrectSumParseDate_InvalidFormat_ThrowsFormatExceptionGetUser_UserDoesNotExist_ReturnsNull"123456789"const INVALID_IDENTITY_NUMBER = "123456789"42const MAX_RETRY_ATTEMPTS = 42"123456789"const INVALID_IDENTITY_NUMBER = "123456789"42const MAX_RETRY_ATTEMPTS = 42| Double Type | Purpose |
|---|---|
| Stub | Returns predefined data for a dependency; used to control the test environment |
| Mock | Records calls and verifies that expected interactions occurred; used for behavioral verification |
| Spy | Like a mock but wraps the real object; allows partial verification without full replacement |
| Fake | A lightweight working implementation (e.g., an in-memory repository) used when stubs are too simple |
| Dummy | A placeholder passed to satisfy a parameter; never actually used in the test |
references/test-doubles.md| Double类型 | 用途 |
|---|---|
| Stub | 为依赖返回预定义数据;用于控制测试环境 |
| Mock | 记录调用并验证是否发生了预期的交互;用于行为验证 |
| Spy | 类似Mock,但包装真实对象;允许在不完全替换对象的情况下进行部分验证 |
| Fake | 轻量级的可工作实现(例如内存中的存储库),当Stub过于简单时使用 |
| Dummy | 用于满足参数要求的占位符;在测试中从未被实际使用 |
references/test-doubles.mdifforwhileswitchifforwhileswitch| Pitfall | Why It Hurts | Remedy |
|---|---|---|
| Complex logic in tests | Tests become buggy and untrustworthy | Keep cyclomatic complexity near 1 |
| Multiple acts in one test | Failures are ambiguous | One act per test method |
| Testing implementation details | Tests break on refactoring | Test through the public interface |
| Non-deterministic tests | Developers lose trust in the suite | Eliminate time, randomness, and shared state dependencies |
| Magic literals | Intent is obscured | Use named constants |
| Mirroring implementation logic | Tests can't catch bugs in the logic | Use independent, fixed expected values |
| Slow tests | Tests are run infrequently | Mock external dependencies; keep tests simple |
| Missing teardown | Tests pollute each other's environments | Always restore state after each test |
| 陷阱 | 危害 | 解决方法 |
|---|---|---|
| 测试中包含复杂逻辑 | 测试代码会出现bug且不可信 | 将圈复杂度保持在1左右 |
| 单个测试包含多个Act步骤 | 失败原因模糊 | 每个测试方法仅包含一个Act步骤 |
| 针对实现细节测试 | 重构时测试会失败 | 通过公共接口进行测试 |
| 非确定性测试 | 开发者失去对测试套件的信任 | 消除对时间、随机性和共享状态的依赖 |
| 魔法值 | 代码意图模糊 | 使用命名常量 |
| 复制实现逻辑 | 测试无法发现逻辑中的bug | 使用独立的固定预期值 |
| 测试执行缓慢 | 测试被少运行 | Mock外部依赖;保持测试简单 |
| 缺少Teardown步骤 | 测试相互污染环境 | 每次测试后始终恢复状态 |
Method_State_ExpectedifforwhileMethod_State_Expectedifforwhilereferences/test-doubles.mdreferences/test-patterns.mdreferences/test-doubles.mdreferences/test-patterns.md