All Projects → MakeAWishFoundation → Swiftymocky

MakeAWishFoundation / Swiftymocky

Licence: mit
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.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftymocky

Testable Mock
换种思路写Mock,让单元测试更简单
Stars: ✭ 977 (+30.61%)
Mutual labels:  mock, unittest
Fake Xrm Easy
The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
Stars: ✭ 216 (-71.12%)
Mutual labels:  mock, unittest
Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (-86.63%)
Mutual labels:  mock, unittest
local-data-api
Data API for local, you can write unittest for AWS Aurora Serverless's Data API
Stars: ✭ 99 (-86.76%)
Mutual labels:  mock, unittest
Pytruth
Assertion framework for Python unit tests
Stars: ✭ 118 (-84.22%)
Mutual labels:  mock, unittest
emock
🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)
Stars: ✭ 73 (-90.24%)
Mutual labels:  mock, unittest
Service Pattern Go
Simple clean Go REST API architecture with dependency injection and mocking example, following SOLID principles.
Stars: ✭ 449 (-39.97%)
Mutual labels:  mock
Fes.js
Fes.js 是一套优秀的中后台前端解决方案。提供初始项目、开发调试、Mock接口、编译打包的命令行工具。内置布局、权限、数据字典、状态管理、存储、Api等多个模块。以约定、配置化、组件化的设计思想,让用户仅仅关心用组件搭建页面内容。基于Vue.js,上手简单。经过多个项目中打磨,趋于稳定。
Stars: ✭ 579 (-22.59%)
Mutual labels:  mock
Vue Admin Html
Vue-cli3.0 + Element UI + Spring Boot2.0 + ThinkPHP5.1 + 响应式的后台管理系统 https://lmxdawn.github.io/vue-admin
Stars: ✭ 436 (-41.71%)
Mutual labels:  mock
Bash unit
bash unit testing enterprise edition framework for professionals
Stars: ✭ 419 (-43.98%)
Mutual labels:  unittest
Green
Green is a clean, colorful, fast python test runner.
Stars: ✭ 691 (-7.62%)
Mutual labels:  unittest
Lyrebird
移动应用插件化测试工作台
Stars: ✭ 663 (-11.36%)
Mutual labels:  mock
Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (-28.48%)
Mutual labels:  mock
Smocker
Smocker is a simple and efficient HTTP mock server and proxy.
Stars: ✭ 465 (-37.83%)
Mutual labels:  mock
Msw
Seamless REST/GraphQL API mocking library for browser and Node.js.
Stars: ✭ 7,830 (+946.79%)
Mutual labels:  mock
Seefood
Inspired by HBO's Silicon Valley: SeeFood is an iOS app that uses CoreML to detect various dishes
Stars: ✭ 445 (-40.51%)
Mutual labels:  xcode9
Nose2
The successor to nose, based on unittest2
Stars: ✭ 665 (-11.1%)
Mutual labels:  unittest
Flask Restful Example
flask后端开发接口示例,利用Flask开发后端API接口。包含基本的项目配置、统一响应、MySQL和Redis数据库操作、定时任务、图片生成、项目部署、用户权限认证、报表输出、无限层级生成目录树、阿里云手机验证码验证、微信授权、Celery、单元测试、Drone等模块。
Stars: ✭ 429 (-42.65%)
Mutual labels:  unittest
Swift Viper Module
Xcode template for VIPER Architecture written in Swift 4
Stars: ✭ 527 (-29.55%)
Mutual labels:  xcode9
Android Testing Guide
[Examples] Complete reference for Android Testing with examples.
Stars: ✭ 652 (-12.83%)
Mutual labels:  mock

Platform Docs License Build Status Integration tests

Version Carthage compatible Mint compatible SPM compatible

logo

Join our community on Slack! -> invitation link here

Check out guides, or full documentation

Table of contents

  1. Overview
  2. Current Version
  3. Getting started:
    1. Installing SwiftyMocky CLI
    2. Integrating SwiftyMocky runtime into test target
    3. Generate mocks
  4. Usage:
    1. Marking protocols to be mocked
    2. Stubbing return values for mock methods - Given
    3. Check invocations of methods, subscripts and properties - Verify
    4. Take action when a stubbed method is called - Perform
  5. Documentation
    1. All supported Features
    2. Examples of usage
    3. Roadmap
    4. Authors
    5. License

Overview

SwiftyMocky is Lightweight, strongly typed framework for Mockito-like unit testing experience. As Swift doesn't support reflections well enough to allow building mocks in runtime, library depends on Sourcery, that scans your source code and generates Mocks Swift code for you!

The idea of SwiftyMocky is to automatically mock Swift protocols. The main features are:

  • easy syntax, utilising full power of auto-complete, which makes writing test easier and faster
  • we DO support generics
  • mock implementations generation
  • a way to specify what mock will return (given)
  • possibility to specify different return values for different attributes
  • record stubbed return values sequence
  • verify, whether a method was called on mock or not
  • check method invocations with specified attributes
  • it works with real device

Important!!! Version 4.x.x

Current version has several significant changes. It removes deprecated methods (which might be breaking) and moves CLI to the new repository.

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.

We consider current version as stable. We are moving toward using the new 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.

While 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.

Migration from 3.2.0 and below

The migration is not required, you can keep using SwiftyMocky as you did before. The Legacy setup is described in guides section.

Still, 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 and try:

> swiftymocky migrate

Getting started

To start working with SwiftyMocky you need to:

  1. Install CLI
  2. Integrate SwiftyMocky runtime library
  3. Generate Mocks and add to your test target

1. Installing SwiftyMocky CLI:

Mint 🌱:

> brew install mint
> mint install MakeAWishFoundation/SwiftyMocky-CLI

Marathon 🏃:

> marathon install MakeAWishFoundation/SwiftyMocky-CLI

Make:

Clone from https://github.com/MakeAWishFoundation/SwiftyMockyCLI and run make in the root directory.

2. Integrating SwiftyMocky runtime into test target:

CocoaPods:

SwiftyMocky is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftyMocky"

Carthage:

To install, add following to you Cartfile:

github "MakeAWishFoundation/SwiftyMocky"

Then execute carthage update

For Carthage, few additional steps are required ⚠️. For detailed install instructions, see full documentation or consult Carthage documentation.

Swift Package Manager:

Add SwiftyMocky to you Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/MakeAWishFoundation/SwiftyMocky", from: "4.0.4"),
]

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

3. Generate mocks

Annotate your protocols that are going to be mocked, making them adopt AutoMockable protocol, or adding annotation comment above their definition in the source code.

Mocks are generated from your project root directory, based on configuration inside Mockfile.

> swiftymocky setup     # if you don't have a Mockfile yet
> swiftymocky doctor    # validate your setup
> swiftymocky generate  # generate mocks

More informations about CLI and mock generation

If you don't want to migrate to our CLI and prefer to use "raw" Sourcery, please refer to this section in documentation.

Usage

1. Marking protocols to be mocked

Create 'dummy' protocol somewhere in your project, like: protocol AutoMockable { }

Adopt it by every protocol you want to actually mock.

protocol ToBeMocked: AutoMockable {
  // ...
}

Alternatively, mark protocols that are meant to be mocked with sourcery annotation as following:

//sourcery: AutoMockable
protocol ToBeMocked {
  // ...
}

Every protocol in source directories, having this annotation, will be added to Mock.generated.swift

2. Stubbing return values for mock methods - Given

All 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).

Generating mock

All 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.

All method attributes are wrapped as Parameter enum, allowing to choose between any and value, giving great flexibility to mock behaviour. Please consider following:

Given(mock, .surname(for name: .value("Johnny"), willReturn: "Bravo"))
Given(mock, .surname(for name: .any, willReturn: "Kowalsky"))

print(mock.surname(for: "Johny"))   // Bravo
print(mock.surname(for: "Mathew"))  // Kowalsky
print(mock.surname(for: "Joanna"))  // Kowalsky

In verions 3.0 we introduced sequences and policies for better control of mock behvaiour.

Given(mock, .surname(for name: .any, willReturn: "Bravo", "Kowalsky", "Nguyen"))

print(mock.surname(for: "Johny"))   // Bravo
print(mock.surname(for: "Johny"))   // Kowalsky
print(mock.surname(for: "Johny"))   // Nguyen
print(mock.surname(for: "Johny"))   // and again Bravo
// ...

For more details please see full documentation.

3. Check invocations of methods, subscripts and properties - Verify

All 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).

Generating mock

All 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.

All method attributes are wrapped as Parameter enum, allowing to choose between any, value and matching, giving great flexibility to tests. Please consider following:

// inject mock to sut. Every time sut saves user data, it should trigger storage storeUser method
sut.usersStorage = mockStorage
sut.saveUser(name: "Johny", surname: "Bravo")
sut.saveUser(name: "Johny", surname: "Cage")
sut.saveUser(name: "Jon", surname: "Snow")

// check if Jon Snow was stored at least one time
Verify(mockStorage, .storeUser(name: .value("Jon"), surname: .value("Snow")))
// storeUser method should be triggered 3 times in total, regardless of attributes values
Verify(mockStorage, 3, .storeUser(name: .any, surname: .any))
// storeUser method should be triggered 2 times with name Johny
Verify(mockStorage, 2, .storeUser(name: .value("Johny"), surname: .any))
// storeUser method should be triggered at least 2 times with name longer than 3
Verify(mockStorage, .moreOrEqual(to: 2), .storeUser(name: .matching({ $0.count > 3 }), surname: .any))

For 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.

From 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:

mock.name = "Danny"
mock.name = "Joanna"

print(mock.name)

// Verify getter:
Verify(mock, 1, .name)
// Verify setter:
Verify(mock, 2, .name(set: .any))
Verify(mock, 1, .name(set: .value("Danny")))
Verify(mock, .never, .name(set: .value("Bishop")))

4. Take action when a stubbed method is called - Perform

All 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.

It uses same parameter wrapping features as given, so you can specify different Perform cases for different attributes set.

It's very handy when working with completion block based approach.

Example:

// Perform allows to execute given closure, with all the method parameters, as soon as it is being called
Perform(mock, .methodThatTakesCompletionBlock(completion: .any, perform: { completion in
    completion(true,nil)
}))

Documentation

Full documentation is available here, as well as through docs directory.

Guides - Table of contents

Changelog is available here

All supported features

For list all supported features, check documentation here or guides

Examples of usage

For more examples, check out our example project, or examples section in guides.

To run the example project, clone the repo, and run pod install from the Example directory first.

To trigger mocks generation, run rake mock or swiftymocky generate from root directory (if you installed CLI).

Roadmap

  • [x] stubbing protocols in elegant way
  • [x] template for generating mocks
  • [x] example project
  • [x] stubbing protocols with variables
  • [x] method signature generation without name conflicts
  • [ ] cover 95% of framework codebase with unit tests
  • [x] cover 95% of framework codebase with documentation
  • [ ] add unit tests for template
  • [x] support for tvOS, Linux and MacOS
  • [x] Carthage support
  • [x] Subscripts support
  • [x] Stub return values as sequences
  • [x] Simple tool simplifying configuration process

Authors

License

SwiftyMocky is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].