{"id":2121,"url":"https://github.com/sabirvirtuoso/Mockit","last_synced_at":"2025-08-06T14:32:53.055Z","repository":{"id":47604110,"uuid":"59012348","full_name":"sabirvirtuoso/Mockit","owner":"sabirvirtuoso","description":"A simple mocking framework for Swift, inspired by the famous http://mockito.org/","archived":false,"fork":false,"pushed_at":"2019-06-21T17:21:09.000Z","size":230,"stargazers_count":120,"open_issues_count":6,"forks_count":27,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-18T04:44:19.399Z","etag":null,"topics":["mocking-framework","mockit","swift"],"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/sabirvirtuoso.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-17T10:16:49.000Z","updated_at":"2024-04-30T20:17:19.000Z","dependencies_parsed_at":"2022-09-12T08:20:41.967Z","dependency_job_id":null,"html_url":"https://github.com/sabirvirtuoso/Mockit","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FMockit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FMockit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FMockit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabirvirtuoso%2FMockit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sabirvirtuoso","download_url":"https://codeload.github.com/sabirvirtuoso/Mockit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228915455,"owners_count":17991408,"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":["mocking-framework","mockit","swift"],"created_at":"2024-01-05T20:16:05.060Z","updated_at":"2024-12-09T15:30:48.873Z","avatar_url":"https://github.com/sabirvirtuoso.png","language":"Swift","funding_links":[],"categories":["Testing","Libs","Testing [🔝](#readme)"],"sub_categories":["Other Testing","Testing","Other free courses"],"readme":"# Mockit\n\n[![Build Status](https://travis-ci.org/sabirvirtuoso/Mockit.svg?branch=master)](https://travis-ci.org/sabirvirtuoso/Mockit)\n[![Coverage Status](https://coveralls.io/repos/github/sabirvirtuoso/Mockit/badge.svg?branch=master)](https://coveralls.io/github/sabirvirtuoso/Mockit?branch=master)\n[![Code Climate](https://codeclimate.com/github/sabirvirtuoso/Mockit/badges/gpa.svg)](https://codeclimate.com/github/sabirvirtuoso/Mockit)\n[![Version](https://img.shields.io/cocoapods/v/Mockit.svg?style=flat)](http://cocoapods.org/pods/Mockit)\n[![License](https://img.shields.io/cocoapods/l/Mockit.svg?style=flat)](http://cocoapods.org/pods/Mockit)\n[![Platform](https://img.shields.io/cocoapods/p/Mockit.svg?style=flat)](http://cocoapods.org/pods/Mockit)\n[![Swift Version](https://img.shields.io/badge/Swift-5.0-F16D39.svg?style=flat)](https://developer.apple.com/swift)\n\n![Mockit GIF](Mockit.png)\n\n## Introduction\n\n`Mockit` is a **Tasty mocking framework for unit tests in Swift 5.0**. It's at an early stage of development, but its current features are almost completely usable.\n\n`Mockit` is a mocking framework that tastes brilliant. It lets you write beautiful tests with a clean \u0026 simple API. Tests written using `Mockit` are very readable and they produce clean verification errors. It's inspired by the famous mocking framework for Java - [Mockito](http://mockito.org/).\n\n## Documentation\n\n`Mockit` is yet to be documented fully but it comes with a sample project that lets you try all its features and become familiar with the API. You can find it in `Mockit.xcworkspace`. \n\nThere's an example test file called ```ExampleTests.swift```. Look there for some tests that can be run. This tests a class ```Example``` against a mocked collaborator ```ExampleCollaborator```. \n\nFile an issue if you have any question.\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Limitations\n\n* There's some boiler-plate code needed to create mocks. See ```MockExampleCollaborator``` for an example in the *Basic Usage* section below. However, a plugin development is on its way for both `Xcode` and `AppCode` to minimize writing this boiler-plate code every time a mock needs to be created.\n\n## Features\n\n- **Stubbing**. *Mockit* lets you stub a method and then perform any of 3 actions (`thenReturn`, `thenDo`, `thenAnswer`) individually or in any order via chaining;\n- **Mocking**. You can create a subclass extending the ```Mock``` protocol to mock required methods;\n- **Call Verification**. You can verify method calls using one of 8 supported modes (`Once`, `AtLeastOnce`, `AtMostOnce`, `Times`, `AtLeastTimes`, `AtMostTimes`, `Never` and `Only`);\n- **Arguments of specific call**. *Mockit* allows you to obtain the arguments of a specific method call to use custom assertions on them;\n- **Helpful messages**. If method verification fails or something goes wrong, Mockit provides readable messages that describes the issue;\n- **Default Type matchers**. Out of the box, *Mockit* can match the following types:\n\n  * String / String?\n  * Bool / Bool?\n  * Int / Int?\n  * Double / Double?\n  * Float / Float?\n  * Array / Array? of the above primitive types\n  * Dictionary / Dictionary? of the above primitive types\n\nGiven that Swift does not have reflection, *Mockit* cannot magically match your custom types, so you need to subclass ```TypeMatcher``` protocol to write \nyour one custom type matcher. For an example, see the *Basic Usage* section below.\n\n## Basic Usage\n\nThe examples below assume we are mocking this class:\n\n```swift\nclass ExampleCollaborator {\n\n    func voidFunction() {\n    }\n\n    func function(int: Int, _ string: String) -\u003e String {\n      return \"\"\n    }\n\n    func stringDictFunction(dict: [String: String]) -\u003e String {\n      return \"\"\n    }\n\n}\n```\n\nIn your test code, you'll need to create a ```MockExampleCollaborator```, which extends ```ExampleCollaborator``` and adopts ```Mock```. The mock creates a ```CallHandler```, and forwards all calls to it:\n\n```swift\nclass MockExampleCollaborator: ExampleCollaborator, Mock {\n\n    let callHandler: CallHandler\n\n    init(testCase: XCTestCase) {\n      callHandler = CallHandlerImpl(withTestCase: testCase)\n    }\n\n    func instanceType() -\u003e MockExampleCollaborator {\n      return self\n    }\n\n    override func voidFunction() {\n      callHandler.accept(nil, ofFunction: #function, atFile: #file, inLine: #line, withArgs: nil)\n    }\n\n    override func function(int: Int, _ string: String) -\u003e String {\n      return callHandler.accept(\"\", ofFunction: #function, atFile: #file, inLine: #line, withArgs: int, string) as! String\n    }\n\n    override func stringDictFunction(dict: Dictionary\u003cString, String\u003e) -\u003e String {\n      return callHandler.accept(\"\", ofFunction: #function, atFile: #file, inLine: #line, withArgs: dict) as! String\n    }\n\n}\n```\n\nTo write a custom type matcher: \n\n```swift\npublic class CustomMatcher: TypeMatcher {\n\n    public func match(argument arg: Any, withArgument withArg: Any) -\u003e Bool {\n      switch (arg, withArg) {\n        case ( _ as CustomType, _ as CustomType):\n          // custom matching code here\n          return true\n        default:\n          return false\n      }\n    }\n\n}\n```\n\nIt is good practice to put the mock objects and custom type matchers in a separate group in the test part of your project.\n\n### Currently-supported syntax\n\n```swift\n// stub a call on a method with parameters, then return value\nmockCollaborator.when().call(withReturnValue: mockCollaborator.function(42, \"frood\")).thenReturn(\"hoopy\")\n```\n\n```swift\n// stub a call on a method with dictionary parameter, then answer value\nmockCollaborator.when().call(withReturnValue: mockCollaborator.stringDictFunction([\"Hello\": \"Pong\"])).thenAnswer {\n      (args: [Any?]) -\u003e String in\n\n      // custom code here\n    }\n```\n\n```swift\n// stub a call on a void method , then do action\nmockCollaborator.when().call(withReturnValue: mockCollaborator.voidFunction()).thenDo {\n      (args: [Any?]) -\u003e Void in\n\n      // if the call is received, this closure will be executed\n      print(\"===== thenDo closure called =====\")\n    }\n```\n\n```swift\n// stub a call on a method , then return values on multiple calls\nmockCollaborator.when().call(withReturnValue: mockCollaborator.stringDictFunction([\"Hello\": \"Pong\"])).thenReturn(\"ping\", \"hoopy\")\n```\n\n```swift\n// stub a call on a method , then chain multiple actions for corresponding calls\nmockCollaborator.when().call(withReturnValue: mockCollaborator.stringDictFunction([\"Hello\": \"Pong\"])).thenReturn(\"ping\", \"hoopy\").thenAnswer {\n      (args: [Any?]) -\u003e String in\n\n      // custom code here\n    }\n```\n\n```swift\n// call a method and then get arguments of a specific call which can be asserted later\nsystemUnderTest.doSomethingWithParamters(42, \"frood\")\nsystemUnderTest.doSomethingWithParamters(18, \"hoopy\")\n\nlet argumentsOfFirstCall = mockCollaborator.getArgs(callOrder: 1).of(mockCollaborator.function(AnyValue.int, AnyValue.string))\nlet argumentsOfSecondCall = mockCollaborator.getArgs(callOrder: 2).of(mockCollaborator.function(AnyValue.int, AnyValue.string))\n```\n\nWith nice mocks, Mockit supports the **\"verify expectations after mocking\"** style using **8 supported verification modes**\n\n```swift\n// call method on the system under test\nsystemUnderTest.expectMethodOneAndThree()\n\n// Then\nmockCollaborator.verify(verificationMode: Once()).methodOne()\nmockCollaborator.verify(verificationMode: Never()).methodTwo()\nmockCollaborator.verify(verificationMode: Once()).methodThree()\n\n\n// call method on the system under test\nsut.expectMethodOneTwice()\n\n// Then\nmockCollaborator.verify(verificationMode: Times(times: 2)).methodOne()\n\n\n// call method on the system under test\nsut.expectOnlyMethodThree()\n\n// Then\nmockCollaborator.verify(verificationMode: Only()).methodThree()\n\n\n// call method on system under test\nsut.expectAllThreeMethods()\n\n// Then\nmockCollaborator.verify(verificationMode: Once()).methodOne()\nmockCollaborator.verify(verificationMode: AtLeastOnce()).methodTwo()\nmockCollaborator.verify(verificationMode: AtMostOnce()).methodThree()\n\n\n// call method on the system under test\nsut.expectMethodTwoAndThree()\n\n// Then\nmockCollaborator.verify(verificationMode: AtLeastTimes(times: Times(times: 1))).methodTwo()\nmockCollaborator.verify(verificationMode: Never()).methodOne()\nmockCollaborator.verify(verificationMode: AtMostTimes(times: Times(times: 3))).methodThree()\n```\n## Requirements\n\n* Xcode 9+\n* XCTest\n\n## Installation\n\nMockit is built with Swift 5.0.\n\n#### CocoaPods\n\nMockit is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'Mockit', '1.5.0'\n```\n\n#### Manually\n1. Download and drop ```/Mockit``` folder in your project.  \n2. Congratulations!  \n\n## Feedback\n\nIssues and pull-requests are most welcome - especially about improving the API further.\n\n## Author\n\nSyed Sabir Salman-Al-Musawi, sabirvirtuoso@gmail.com\n\nI'd also like to thank [**Sharafat Ibn Mollah Mosharraf**](sharafat_8271@yahoo.co.uk) for his big support during the API design and development phase.\n\n## License\n\n**Mockit** is available under the **MIT license**. See the `LICENSE` file for more info.\n\nThe PNG at the top of this `README.md` is from [www.mockit.co.uk/about.html](http://www.mockit.co.uk/about.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabirvirtuoso%2FMockit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsabirvirtuoso%2FMockit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabirvirtuoso%2FMockit/lists"}