Sitemap

Straight to the point: MethodTesting

2 min readMay 8, 2025

MethodTesting is an approach to implementing unit tests in Swift, specifically designed to fill the gaps left by Apple’s new Swift Testing framework. While Swift Testing introduces a modern and expressive API, it does not directly support the old expectation model from XCTest, making it challenging to write tests involving complex integrations between asynchronous objects.

To address this issue, MethodTesting leverages the concept of AsyncSignal to manage asynchronous events safely and concisely. This allows for the creation of more robust and scalable tests, especially in scenarios where multiple tasks need to wait for an operation to complete before proceeding.

What is MethodTesting?

MethodTesting is a generic class that allows testing asynchronous methods through a waiting and signaling mechanism. It integrates seamlessly with Swift’s asynchronous concurrency model, using AsyncSignal to efficiently manage events.

The implementation of MethodTesting is based on two main methods:

  1. wait(for:): Suspends the current task until a specific event is signaled.
  2. fulfill(_:): Releases the signal, allowing pending tasks to resume execution.

MethodTesting is implemented as a generic class that accepts an enumerated type (enum) as a parameter. This enum defines the methods or events to be tested.

The example below demonstrates how MethodTesting is used in unit tests to verify the integration between a ViewModel and a Coordinator.

Advantages of MethodTesting

  1. Simplicity: MethodTesting provides a concise and intuitive API for managing asynchronous events.
  2. Safety: The use of AsyncSignal ensures protection against concurrency issues, preventing unsafe simultaneous access to shared resources.
  3. Flexibility: It allows testing complex scenarios involving multiple integrations between objects.
  4. Compatibility: Works seamlessly with Apple’s modern Swift Testing framework.

Conclusion

MethodTesting is an elegant solution to fill the gaps left by Apple’s new testing framework. It simplifies the implementation of unit tests involving complex integrations, ensuring more readable, safe, and scalable code. With MethodTesting, you can create more robust and reliable tests, even in challenging asynchronous scenarios.

If you would like to contribute so that I can continue producing more technical content, please feel free to buy me a coffee ☕️ through the Buy me a Coffee platform.

Your support is essential to maintain my work and contribute to the development community.

--

--

Brenno de Moura
Brenno de Moura

Written by Brenno de Moura

Software engineer with a passion for technology and a focus on declarative programming, experience in challenging projects and multidisciplinary teams

No responses yet