{"id":15038672,"url":"https://github.com/makeawishfoundation/swiftymocky","last_synced_at":"2025-05-15T12:04:25.854Z","repository":{"id":38751806,"uuid":"100740280","full_name":"MakeAWishFoundation/SwiftyMocky","owner":"MakeAWishFoundation","description":"Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.","archived":false,"fork":false,"pushed_at":"2024-09-02T12:14:45.000Z","size":25354,"stargazers_count":1063,"open_issues_count":65,"forks_count":117,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-05-15T12:04:18.976Z","etag":null,"topics":["generating-mocks","mock","swift","unittest","xcode9"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MakeAWishFoundation.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":"docs/supported-features.html","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-18T18:34:55.000Z","updated_at":"2025-04-24T02:44:32.000Z","dependencies_parsed_at":"2024-06-18T19:48:39.105Z","dependency_job_id":"cef47eeb-1156-4c4f-b4bd-c6b6638a22d7","html_url":"https://github.com/MakeAWishFoundation/SwiftyMocky","commit_stats":{"total_commits":376,"total_committers":34,"mean_commits":"11.058823529411764","dds":"0.48404255319148937","last_synced_commit":"d0f1bd4e8f11c85727d6a5af752d44669962e00b"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MakeAWishFoundation%2FSwiftyMocky","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MakeAWishFoundation%2FSwiftyMocky/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MakeAWishFoundation%2FSwiftyMocky/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MakeAWishFoundation%2FSwiftyMocky/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MakeAWishFoundation","download_url":"https://codeload.github.com/MakeAWishFoundation/SwiftyMocky/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337612,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generating-mocks","mock","swift","unittest","xcode9"],"created_at":"2024-09-24T20:39:37.363Z","updated_at":"2025-05-15T12:04:20.822Z","avatar_url":"https://github.com/MakeAWishFoundation.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Platform](https://img.shields.io/cocoapods/p/SwiftyMocky.svg?style=flat)](http://cocoapods.org/pods/SwiftyMocky)\n[![Docs](https://raw.githubusercontent.com/MakeAWishFoundation/SwiftyMocky/master/docs/badge.svg)](https://rawcdn.githack.com/MakeAWishFoundation/SwiftyMocky/master/docs/index.html)\n[![License](https://img.shields.io/cocoapods/l/SwiftyMocky.svg?style=flat)](http://cocoapods.org/pods/SwiftyMocky)\n\n[![Pods compatible](https://img.shields.io/cocoapods/v/SwiftyMocky.svg?style=flat\u0026color=green)](http://cocoapods.org/pods/SwiftyMocky)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-green.svg?style=flat)](https://github.com/Carthage/Carthage)\n![Mint compatible](https://img.shields.io/badge/🌱%20Mint-compatible-green.svg)\n![SPM compatible](https://img.shields.io/badge/SwiftPM-compatible-green.svg?style=flat\u0026logo=swift)\n\n[![Build \u0026 Test](https://github.com/MakeAWishFoundation/SwiftyMocky/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/MakeAWishFoundation/SwiftyMocky/actions?query=branch%3Amaster) [![CocoaPods Integration](https://github.com/MakeAWishFoundation/SM-Integration-Tests/actions/workflows/cocoapods.yml/badge.svg?branch=master)](https://github.com/MakeAWishFoundation/SM-Integration-Tests/actions/workflows/cocoapods.yml) [![SwiftPM Integration](https://github.com/MakeAWishFoundation/SM-Integration-Tests/actions/workflows/swiftpm.yml/badge.svg?branch=master)](https://github.com/MakeAWishFoundation/SM-Integration-Tests/actions/workflows/swiftpm.yml) [![Carthage Integration](https://github.com/MakeAWishFoundation/SM-Integration-Tests/actions/workflows/carthage.yml/badge.svg?branch=master)](https://github.com/MakeAWishFoundation/SM-Integration-Tests/actions/workflows/carthage.yml)\n\n# ![logo][logo]\n\nCheck out [guides][link-guides-contents], or full [documentation][link-docs]\n\n## Table of contents\n\n1. [Overview](#overview)\n1. [Current Version](#current-version)\n1. [Getting started:](#getting-started)\n    1. [Integrating SwiftyMocky runtime into test target](#integration)\n    1. [Installation SwiftyMocky CLI](#installation)\n    1. [Generate mocks](#generation)\n1. [Usage:](#usage)\n    1. [Marking protocols to be mocked](#mock-annotate)\n    1. [Stubbing return values for mock methods - Given](#given)\n    1. [Check invocations of methods, subscripts and properties - Verify](#verify)\n    1. [Take action when a stubbed method is called - Perform](#perform)\n1. [Documentation](#guides)\n    1. [All supported Features](#features)\n    1. [Examples of usage](#examples)\n    1. [Roadmap](#roadmap)\n    1. [Authors](#authors)\n    1. [License](#license)\n\n\u003ca name=\"overview\"\u003e\u003c/a\u003e\n\n## Overview\n\n**SwiftyMocky** is a strongly typed framework for Mockito-like unit testing experience. Library depends on [Sourcery](https://github.com/krzysztofzablocki/Sourcery), that scans your source code and **generates Mocks Swift code for you!**\n\nThe idea of **SwiftyMocky** is to automatically mock Swift protocols and protocol compositions. The main features are:\n\n - easy syntax, utilising full power of auto-complete, which makes writing test easier and faster\n - **we DO support generics**\n - mock implementations generation\n - a way to specify what mock will return (given)\n - possibility to specify different return values for different attributes\n - record stubbed return values sequence\n - verify, whether a method was called on mock or not\n - check method invocations with specified attributes\n - it works with real device\n\n\u003ca name=\"current-version\"\u003e\u003c/a\u003e\n\n## **Important!!!** Version 4.1.x\n\nCLI was moved bask to the main (this) repo. CLI in this [repository](https://github.com/MakeAWishFoundation/SwiftyMockyCLI) will be supported at least until version 5.0.0.\n\n## Version 4.0.x\n\nCurrent version has several significant changes. It removes deprecated methods (which might be breaking) and deprecates having CLI in the new [repository](https://github.com/MakeAWishFoundation/SwiftyMockyCLI).\n\n**SwiftyPrototype** was also extracted to separate library. There are no more compilation flags, so if you were relying on **SwiftyMocky** with `-DMockyCustom`, you will have to switch to `SwiftyPrototype`.\n\nWe consider current version as stable. We are moving toward using the new [Mockfile][link-guides-mockfile] but the previous configuration format would be still supported. Library works with Swift **4.1, 4.2, 5.0, 5.1.2**  and  Sourcery 1.0.x.\n\nWhile it is technically possible to integrate SwiftyMocky on Linux targets, there is no Mock generation feature there yet. You can use SwiftyMokcy runtime via SwiftPM though, as long as your are fine with generating mocks on mac machine.\n\n## Migration from 3.2.0 and below\n\nThe migration is not required, you can keep using **SwiftyMocky** as you did before. The [Legacy setup](https://github.com/MakeAWishFoundation/SwiftyMocky/blob/master/guides/Legacy.md) is described in [guides section](https://github.com/MakeAWishFoundation/SwiftyMocky/blob/master/guides/Contents.md).\n\nStill, we would encourage to try new **CLI** and share a feedback. We believe it will make using and setting up **SwiftyMocky** way easier. If you have an existing setup, install CLI as per this [guide](https://github.com/MakeAWishFoundation/SwiftyMocky/blob/master/guides/Installation.md) and try:\n\n```bash\n\u003e swiftymocky migrate\n```\n\n\u003ca name=\"getting-started\"\u003e\u003c/a\u003e\n\n## Getting started\n\n\u003ca name=\"integration\"\u003e\u003c/a\u003e\n\n### 1. Integrating SwiftyMocky:\n\n**[CocoaPods](http://cocoapods.org)**:\n\nSwiftyMocky is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:\n\n```ruby\npod \"SwiftyMocky\"\n```\n\nUse CLI tool from your project directory:\n\n```zsh\n# To setup initial Mockfile\n% ./Pods/SwiftyMocky/bin/swiftymocky setup\n# To generate mocks\n% ./Pods/SwiftyMocky/bin/swiftymocky generate\n```\n\n**[Carthage](https://github.com/Carthage/Carthage)**:\n\nTo install, add following to you Cartfile:\n\n```ruby\ngithub \"MakeAWishFoundation/SwiftyMocky\"\n```\n\nThen execute `carthage update`\n\nFor [Carthage](https://github.com/Carthage/Carthage), few additional steps are required ⚠️. For detailed install instructions, see full [documentation][link-docs-installation-carthage] or consult [Carthage documentation][carthage-adding-framework].\n\nYou need to install CLI to generate mocks - see [installation](#installation)\n\n**[Swift Package Manager](https://swift.org/package-manager/)**:\n\nAdd **SwiftyMocky** to you **Package.swift** dependencies:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/MakeAWishFoundation/SwiftyMocky\", from: \"4.2.0\"),\n]\n```\n\nYou need to install CLI to generate mocks - see [installation](#installation)\n\n\u003e **Note:** Examples of **SwiftyMocky** integration as a tool for Unit tests, as well as a Prototyping framework, are here: [https://github.com/MakeAWishFoundation/SM-Integration-Tests](https://github.com/MakeAWishFoundation/SM-Integration-Tests)\n\n\u003ca name=\"installation\"\u003e\u003c/a\u003e\n\n### 2. Installing SwiftyMocky CLI:\n\n**[Mint 🌱](https://github.com/yonaskolb/Mint)**:\n\n```bash\n\u003e brew install mint\n\u003e mint install MakeAWishFoundation/SwiftyMocky\n```\n\n**[Marathon 🏃](https://github.com/JohnSundell/Marathon)**:\n\n```bash\n\u003e marathon install MakeAWishFoundation/SwiftyMocky\n```\n\n**Make**:\n\nClone from https://github.com/MakeAWishFoundation/SwiftyMockyCLI and run `make` in the root directory.\n\n-----\n\n\u003ca name=\"generation\"\u003e\u003c/a\u003e\n\n### 3. Generate mocks\n\n[Annotate your protocols](#mock-annotate) that are going to be mocked, making them adopt `AutoMockable` protocol, or adding annotation comment above their definition in the source code.\n\nMocks are generated from your project root directory, based on configuration inside [Mockfile][link-guides-mockfile].\n\n```bash\n\u003e path/to/swiftymocky setup     # if you don't have a Mockfile yet\n\u003e path/to/swiftymocky doctor    # validate your setup\n\u003e path/to/swiftymocky generate  # generate mocks\n```\n\nMore informations about [CLI][link-guides-cli] and [mock generation][link-guides-cli-generate]\n\nIf you don't want to migrate to our **CLI** and prefer to use \"raw\" Sourcery, please refer [to this section in documentation][link-guides-cli-legacy].\n\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n# Usage\n\n\u003ca name=\"mock-annotate\"\u003e\u003c/a\u003e\n\n## 1. Marking protocols to be mocked\n\nCreate 'dummy' protocol somewhere in your project, like: `protocol AutoMockable { }`\n\nAdopt it by every protocol you want to actually mock.\n\n```swift\nprotocol ToBeMocked: AutoMockable {\n  // ...\n}\n```\n\nAlternatively, mark protocols that are meant to be mocked with sourcery annotation as following:\n\n```swift\n//sourcery: AutoMockable\nprotocol ToBeMocked {\n  // ...\n}\n```\n\nOr use it to protocol compositions:\n\n```swift\ntypealias ToBeMocked = OneProtocol \u0026 TwoProtocols \u0026 AutoMockable\n```\n\nEvery protocol in source directories, having this annotation, will be added to `Mock.generated.swift`\n\n\u003ca name=\"given\"\u003e\u003c/a\u003e\n\n## 2. Stubbing return values for mock methods - **Given**\n\nAll mocks has **given** method (accessible both as instance method or global function), with easy to use syntax, allowing to specify what should be return values for given methods (based on specified attributes).\n\n![Generating mock][example-given]\n\nAll protocol methods are nicely put into **Given**, with matching signature. That allows to use auto-complete (just type `.`) to see all mocked protocol methods, and specify return value for them.\n\nAll method attributes are wrapped as **Parameter** enum, allowing to choose between `any` and `value`, giving great flexibility to mock behaviour. Please consider following:\n\n```swift\nGiven(mock, .surname(for name: .value(\"Johnny\"), willReturn: \"Bravo\"))\nGiven(mock, .surname(for name: .any, willReturn: \"Kowalsky\"))\n\nprint(mock.surname(for: \"Johny\"))   // Bravo\nprint(mock.surname(for: \"Mathew\"))  // Kowalsky\nprint(mock.surname(for: \"Joanna\"))  // Kowalsky\n```\n\nIn verions 3.0 we introduced sequences and policies for better control of mock behvaiour.\n\n```swift\nGiven(mock, .surname(for name: .any, willReturn: \"Bravo\", \"Kowalsky\", \"Nguyen\"))\n\nprint(mock.surname(for: \"Johny\"))   // Bravo\nprint(mock.surname(for: \"Johny\"))   // Kowalsky\nprint(mock.surname(for: \"Johny\"))   // Nguyen\nprint(mock.surname(for: \"Johny\"))   // and again Bravo\n// ...\n```\n\nFor more details please see full [documentation][link-docs].\n\n\u003ca name=\"verify\"\u003e\u003c/a\u003e\n\n## 3. Check invocations of methods, subscripts and properties - **Verify**\n\nAll mocks has **verify** method (accessible both as instance method or global function), with easy to use syntax, allowing to verify, whether a method was called on mock, and how many times. It also provides convenient way to specify, whether method attributes matters (and which ones).\n\n![Generating mock][example-verify]\n\nAll protocol methods are nicely put into **Verify**, with matching signature. That allows to use auto-complete (just type `.`) to see all mocked protocol methods, and specify which one we want to verify.\n\nAll method attributes are wrapped as **Parameter** enum, allowing to choose between `any`, `value` and `matching`, giving great flexibility to tests. Please consider following:\n\n```swift\n// inject mock to sut. Every time sut saves user data, it should trigger storage storeUser method\nsut.usersStorage = mockStorage\nsut.saveUser(name: \"Johny\", surname: \"Bravo\")\nsut.saveUser(name: \"Johny\", surname: \"Cage\")\nsut.saveUser(name: \"Jon\", surname: \"Snow\")\n\n// check if Jon Snow was stored at least one time\nVerify(mockStorage, .storeUser(name: .value(\"Jon\"), surname: .value(\"Snow\")))\n// storeUser method should be triggered 3 times in total, regardless of attributes values\nVerify(mockStorage, 3, .storeUser(name: .any, surname: .any))\n// storeUser method should be triggered 2 times with name Johny\nVerify(mockStorage, 2, .storeUser(name: .value(\"Johny\"), surname: .any))\n// storeUser method should be triggered at least 2 times with name longer than 3\nVerify(mockStorage, .moreOrEqual(to: 2), .storeUser(name: .matching({ $0.count \u003e 3 }), surname: .any))\n```\n\nFor **Verify**, you can use **Count** to specify how many times you expect something to be triggered. **Count** can be defined as explicit value, like `1`,`2`,... or in more descriptive and flexible way, like `.never`, `more(than: 1)`, etc.\n\nFrom SwiftyMocky 3.0, it is possible to use `Given` and perform `Verify` on properties as well, with respect to whether it is get or set:\n\n```swift\nmock.name = \"Danny\"\nmock.name = \"Joanna\"\n\nprint(mock.name)\n\n// Verify getter:\nVerify(mock, 1, .name)\n// Verify setter:\nVerify(mock, 2, .name(set: .any))\nVerify(mock, 1, .name(set: .value(\"Danny\")))\nVerify(mock, .never, .name(set: .value(\"Bishop\")))\n```\n\n\u003ca name=\"perform\"\u003e\u003c/a\u003e\n\n### 4. Take action when a stubbed method is called - **Perform**\n\nAll mocks has **perform** method (accessible both as instance method or global function), with easy to use syntax, allowing to specify closure, that will be executed upon stubbed method being called.\n\nIt uses same parameter wrapping features as given, so you can specify different **Perform** cases for different attributes set.\n\nIt's very handy when working with completion block based approach.\n\nExample:\n\n```swift\n// Perform allows to execute given closure, with all the method parameters, as soon as it is being called\nPerform(mock, .methodThatTakesCompletionBlock(completion: .any, perform: { completion in\n    completion(true,nil)\n}))\n```\n\n\u003ca name=\"guides\"\u003e\u003c/a\u003e\n\n# Documentation\n\nFull documentation is available [here][link-docs], as well as through *docs* directory.\n\nGuides - [Table of contents][link-guides-contents]\n\nChangelog is available [here][link-changelog]\n\n\u003ca name=\"features\"\u003e\u003c/a\u003e\n\n## All supported features\n\nFor list all supported features, check documentation [here][link-docs-features] or [guides][link-guides-features]\n\n\u003ca name=\"examples\"\u003e\u003c/a\u003e\n\n## Examples of usage\n\nFor more examples, check out our example project, or examples section in [guides][link-guides-examples].\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\nTo trigger mocks generation, run `rake mock` or `swiftymocky generate` from root directory (if you installed CLI).\n\n\u003ca name=\"roadmap\"\u003e\u003c/a\u003e\n\n## Roadmap\n\n- [x] stubbing protocols in elegant way\n- [x] template for generating mocks\n- [x] example project\n- [x] stubbing protocols with variables\n- [x] method signature generation without name conflicts\n- [ ] cover 95% of framework codebase with unit tests\n- [x] cover 95% of framework codebase with documentation\n- [ ] add unit tests for template\n- [x] support for tvOS, Linux and MacOS\n- [x] Carthage support\n- [x] Subscripts support\n- [x] Stub return values as sequences\n- [x] Simple tool simplifying configuration process\n\n\u003ca name=\"authors\"\u003e\u003c/a\u003e\n\n## Authors\n\n- Przemysław Wośko, wosko.przemyslaw@gmail.com\n- Andrzej Michnia, amichnia@gmail.com\n\n\u003ca name=\"license\"\u003e\u003c/a\u003e\n\n## License\n\nSwiftyMocky is available under the MIT license. See the [LICENSE][link-license] file for more info.\n\n\u003c!-- Links --\u003e\n\n[link-slack]: https://join.slack.com/t/swiftymocky/shared_invite/enQtMjkwNDE1NjY5MjA3LTU2YjA4YTI3NDE5MzNkZTU4MzlmMzkwYmUzNzRiNWRlN2U5NmUyMDNkN2U0NGE2ZDkxYTU4NGViYzIxYjc5ZmE\n[link-license]: ./LICENSE\n[link-guides-installation]: ./guides/Setup%20in%20project.md\n[link-guides-setup]: ./guides/Installation.md\n[link-guides-contents]: ./guides/Contents.md\n[link-guides-features]: ./guides/Supported%20features.md\n[link-guides-examples]: ./guides/Examples.md\n[link-changelog]: ./guides/CHANGELOG.md\n\n[link-guides-cli]: ./guides/Command%20Line%20Interface.md\n[link-guides-cli-migration]: ./guides/Command%20Line%20Interface.md#migration\n[link-guides-cli-legacy]: ./guides/Legacy.md\n[link-guides-cli-generate]: ./guides/Command%20Line%20Interface.md#generate\n[link-guides-mockfile]: ./guides/Mockfile.md\n\n[carthage-adding-framework]: https://github.com/Carthage/Carthage#adding-frameworks-to-unit-tests-or-a-framework\n\n\u003c!-- Links based on tag --\u003e\n\n[link-docs]: https://rawcdn.githack.com/MakeAWishFoundation/SwiftyMocky/4.2.0/docs/index.html\n[link-docs-features]: https://rawcdn.githack.com/MakeAWishFoundation/SwiftyMocky/4.2.0/docs/supported-features.html\n[link-docs-installation]: https://rawcdn.githack.com/MakeAWishFoundation/SwiftyMocky/4.2.0/docs/installation.html\n[link-docs-installation-carthage]: https://rawcdn.githack.com/MakeAWishFoundation/SwiftyMocky/4.2.0/docs/installation.html#installation-carthage\n[link-docs-setup]: https://rawcdn.githack.com/MakeAWishFoundation/SwiftyMocky/4.2.0/docs/setup-in-project.html\n\n\u003c!-- Assets --\u003e\n\n[logo]: https://raw.githubusercontent.com/MakeAWishFoundation/SwiftyMocky/4.2.0/icon.png\n[example-watcher]: https://raw.githubusercontent.com/MakeAWishFoundation/SwiftyMocky/1.0.0/guides/assets/example-watcher.gif \"Example - generation\"\n[example-given]: https://raw.githubusercontent.com/MakeAWishFoundation/SwiftyMocky/1.0.0/guides/assets/example-given.gif \"Example - given\"\n[example-verify]: https://raw.githubusercontent.com/MakeAWishFoundation/SwiftyMocky/1.0.0/guides/assets/example-verify.gif \"Example - verify\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmakeawishfoundation%2Fswiftymocky","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmakeawishfoundation%2Fswiftymocky","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmakeawishfoundation%2Fswiftymocky/lists"}