Swift Mocking Framework using Swift Macros

Hi everyone!

I'm super excited to share with you a framework I created for mocking using the new Swift Macros. It's called SwiftMocks and it's a very simple and lightweight framework to help mock objects and stub responses in your tests.

I've been following the discussions on this forum about how to use macros for mocking, and I learned a lot from the feedback and suggestions of other developers. However, I also felt that there was a gap between what I wanted to achieve with mocking and what the existing solutions offered. So I decided to create my own framework, while incorporating the feedback I saw.

SwiftMocks is designed to be easy to use, expressive and flexible. You can create mock objects and stub their methods and properties using a fluent syntax. You can also verify the interactions between your mocks and your code under test, and check the arguments passed to the stubbed methods.

One of the advantages of using SwiftMocks is that it leverages the power of macros to generate the mock code at compile time, rather than using runtime reflection or code generation tools. This means that you get type safety, autocomplete and compiler errors when using your mocks, and you don't need to run any extra scripts or tools to generate them.

SwiftMocks is still a work in progress, and I'm looking forward to seeing how macros evolve in Swift. For example, I hope that macros will support adding code to function definitions in the future, which would make using SwiftMocks even easier and more elegant.

I would love to hear your feedback on SwiftMocks, and I invite you to check out the repo I created with the framework: GitHub - frugoman/SwiftMocks: Mocking framework for Swift using Macros

It's an open source project, so feel free to contribute with issues, pull requests or suggestions. I hope you find SwiftMocks useful and fun to use!


Looks exciting! Do you intend to provide a README with usage examples at the root of the repository? There is one in Sources/SwiftMocks, but it may be harder to find

1 Like

woops! just moved it to the root! Thanks for the shout!

I believe your reply is unwarranted. It is putting down a project based purely on personal opinion.

As it happens, I know of a major product that uses a single-protocol-single-conformance approach for their main app. They force this abstraction on developers to keep the code clean and to allow for quicker build times by isolating the protocols and conformances into separate modules. Users of the conformers are not affected when the implementations change.


@Frugoman hello, is this library still maintained? I thought to give it try. looks like simple test , it could not pass.

let user = User(firstName: "Deepak", lastName: "s")
XCTAssertEqual(user.mock.saveNameToUDCalls.callsCount, 1)