Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 887 Bytes

File metadata and controls

34 lines (25 loc) · 887 Bytes

Mocking Strategies for Commands

For testing error scenarios and edge cases, use mocks:

Example

use mockall::predicate::*;

#[test]
fn it_should_handle_infrastructure_failure_gracefully() {
    // Create mock dependencies
    let mut mock_client = MockOpenTofuClient::new();
    mock_client.expect_destroy()
        .times(1)
        .returning(|| Err(OpenTofuError::DestroyFailed));

    let mock_repo = Arc::new(MockEnvironmentRepository::new());

    // Create command with mocks
    let command = DestroyCommand::new(Arc::new(mock_client), mock_repo);

    // Execute and verify error handling
    let result = command.execute(test_environment);
    assert!(matches!(result, Err(DestroyCommandError::OpenTofu(_))));
}

When to Use Mocks

  • Testing error handling paths
  • Validating retry logic
  • Simulating external service failures
  • Testing timeout scenarios