Verifying Interactions with FakeItEasy

Verifying Interactions with FakeItEasy

·

3 min read

Test doubles are objects that are used in place of real objects during testing, allowing developers to isolate and control the behavior of specific parts of their code. One of the key features of FakeItEasy is its ability to verify interactions between test doubles and the code being tested.

Creating a Test Double

To get started with FakeItEasy, you'll need to create a test double of the object you want to replace. This can be done using the A.Fake<T>() method, where T is the type of the object you want to replace. For example, to create a test double of a CustomerRepository class, you would use the following code:

var repository = A.Fake<CustomerRepository>();

Configuring Behavior

Once you have created a test double, you can configure its behavior using the A.CallTo method. This method allows you to specify how the test double should respond when certain methods are called. For example, the following code configures the test double to return a specific customer when the GetById method is called:

A.CallTo(() => repository.GetById(1)).Returns(new Customer { Id = 1, Name = "John Smith" });

Verifying Interactions

FakeItEasy provides several methods for verifying interactions between test doubles and the code being tested. The most commonly used methods are:

  • A.CallTo(...): This method is used to assert that a specific method was called on a test double. For example, the following code verifies that the GetById method was called on the repository test double:
A.CallTo(() => repository.GetById(1)).MustHaveHappened();
  • A.CallToSet(...): This method is used to assert that a specific property was set on a test double. For example, the following code verifies that the IsActive property was set to true on the customer test double:
A.CallToSet(() => customer.IsActive).To(true).MustHaveHappened();

Example

Here's an example of how you might use FakeItEasy to test a CustomerService class that relies on a CustomerRepository class:

[Test]
public void GetCustomer_ShouldReturnCorrectCustomer()
{
    // Arrange
    var repository = A.Fake<CustomerRepository>();
    A.CallTo(() => repository.GetById(1)).Returns(new Customer { Id = 1, Name = "John Smith" });
    var service = new CustomerService(repository);

    // Act
    var customer = service.GetCustomer(1);

    // Assert
    Assert.AreEqual("John Smith", customer.Name);
    A.CallTo(() => repository.GetById(1)).MustHaveHappened();
}

In this example, we first create a test double of the CustomerRepository class using A.Fake<CustomerRepository>(). Next, we configure the test double to return a specific customer when the GetById method is called using A.CallTo(() => repository.GetById(1)).Returns(new Customer { Id = 1, Name = "John Smith" });.

Then we create an instance of CustomerService class by passing the test double of CustomerRepository as a dependency.

In the "Act" section, we call the GetCustomer method on the CustomerService instance and store the returned customer in a variable.

Finally, in the "Assert" section, we check that the name of the returned customer is "John Smith" using the Assert.AreEqual method and also use A.CallTo(() => repository.GetById(1)).MustHaveHappened() to verify that the GetById method was called on the repository test double.

This test confirms that the CustomerService class is correctly using the CustomerRepository class to retrieve the correct customer, and that the GetById method is being called with the correct ID.