Isolating Dependencies with Test Doubles: Dummies

Introduction to Dependencies in Testing

Welcome to the first lesson of our course on managing test doubles. In this lesson, we will explore the concept of dependencies in software testing and introduce you to the use of test doubles, starting with "dummies," the simplest form of test double.

Dependencies are components or services that your software relies on to function, like databases, logging systems, or external APIs. However, when testing, these dependencies can introduce variability, making it hard to test your code's logic reliably. Test doubles allow you to replace these real dependencies with simpler objects that mimic their behavior. This ensures tests focus solely on your code's logic without interference from external systems. For instance, by isolating an email service's logging component using a test double, you can test email-related functionality without generating actual log entries.

During this course, we'll discuss four kinds of test doubles:

  • Dummies: These are simple placeholders used to fulfill parameter requirements. They have no logic or behavior beyond satisfying an interface or method signature.
  • Stubs: These provide predefined responses to specific calls during testing, allowing you to control the behavior of certain dependencies without implementing full functionality.
  • Mocks: These are more sophisticated test doubles that are like stubs but with more capabilities. They allow you to set expectations and verify that certain interactions occur during testing.
  • Fakes: These are simpler implementations of complex behavior that are useful for testing, typically with some working logic, often used to simulate a real system or component.

During this lesson, you'll learn how dummies provide a straightforward way to address dependencies by serving as simple placeholders without any logic. By the end, you'll have a foundational understanding of how to utilize dummies in your workflow, paving the way for more complex test doubles in future lessons.

Example of Using Dummies

Let's see dummies in action by setting up tests for an EmailService application using Rust's testing framework. Here's how you can create a basic test using dummies:

// Trait Definitions
trait Logger {
    fn log(&self, message: &str);
}

trait EmailSender {
    fn send(&self, to: &str, subject: &str, body: &str);
}

// Dummy Implementations
struct DummyLogger;

impl Logger for DummyLogger {
    fn log(&self, _message: &str) {}
}

struct DummyEmailSender;

impl EmailSender for DummyEmailSender {
    fn send(&self, _to: &str, _subject: &str, _body: &str) {}
}

// EmailService Test
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_email_service_should_accept_valid_email_parameters() {
        // Arrange
        let logger = DummyLogger;
        let email_sender = DummyEmailSender;
        let service = EmailService::new(logger, email_sender);

        // Act
        let result = service.send_email("test@example.com", "Hello", "This is a test");

        // Assert
        assert!(result);
    }
}

In this example:

  • DummyLogger and DummyEmailSender act as stand-ins for real implementations. They don't have any behavior; they just satisfy the trait requirements of EmailService.
  • By using dummies, you reduce complexity and ensure that the tests are focusing solely on the logic within EmailService.

This method provides an introduction to isolating dependencies with minimal effort, setting the stage for learning about more advanced test doubles like stubs, mocks, and fakes.

Implementing the EmailService

To fully understand how dummies integrate into your testing workflow, let's look at the implementation of the EmailService that the tests are targeting. Here's how you can set up the EmailService in Rust:

struct EmailService<L: Logger, E: EmailSender> {
    logger: L,
    email_sender: E,
}

impl<L: Logger, E: EmailSender> EmailService<L, E> {
    fn new(logger: L, email_sender: E) -> Self {
        EmailService { logger, email_sender }
    }

    fn send_email(&self, to: &str, subject: &str, body: &str) -> bool {
        if to.is_empty() || !to.contains('@') {
            return false;
        }

        if subject.is_empty() {
            return false;
        }

        if body.is_empty() || body.len() > 1000 {
            return false;
        }

        self.logger.log(&format!("Sending email to {}", to));
        self.email_sender.send(to, subject, body);
        true
    }
}

This implementation outlines a basic EmailService struct, using traits for a logger and an email sender as dependencies. The method send_email performs basic validation checks on the email parameters and, if they're valid, logs and sends the email using the dependencies provided. By using dummies to stand in for Logger and EmailSender, you can isolate this email-sending logic from the complexities of actual logging and email-sending functionalities, ensuring a focused test environment.

Summary and Preparing for Practice

In this lesson, you were introduced to the concept of dependencies in testing and how test doubles, specifically dummies, can help isolate these dependencies. Dummies serve as simple placeholders without behavior, allowing you to concentrate on testing the core logic of your application without external complexities. By using dummies, you can simplify test setups and focus more effectively on the behavior of the code under test. This foundational understanding sets the stage for exploring more advanced test doubles in future lessons.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal