All Projects → leoture → Mockswift

leoture / Mockswift

Licence: mit
MockSwift is a Mock library written in Swift.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Mockswift

Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+94.64%)
Mutual labels:  mock, test, swift-package-manager
Playground
Instantly create Swift playgrounds from the command line
Stars: ✭ 391 (+598.21%)
Mutual labels:  xcode, playground
Shellspec
A full-featured BDD unit testing framework for bash, ksh, zsh, dash and all POSIX shells
Stars: ✭ 375 (+569.64%)
Mutual labels:  test, mock
Playalways
Create Xcode playgrounds from your menu bar
Stars: ✭ 515 (+819.64%)
Mutual labels:  xcode, playground
Fakerest
Patch fetch/XMLHttpRequest to fake a REST API server in the browser, based on JSON data.
Stars: ✭ 350 (+525%)
Mutual labels:  test, mock
Xcbeautify
A little beautifier tool for xcodebuild
Stars: ✭ 372 (+564.29%)
Mutual labels:  xcode, swift-package-manager
Brisk
A proof of concept scripting library for Swift
Stars: ✭ 478 (+753.57%)
Mutual labels:  xcode, swift-package-manager
Mockolo
Efficient Mock Generator for Swift
Stars: ✭ 327 (+483.93%)
Mutual labels:  xcode, mock
Whats New In Swift 5 0
An Xcode playground that demonstrates the new features introduced in Swift 5.0.
Stars: ✭ 703 (+1155.36%)
Mutual labels:  xcode, playground
Swiftlyext
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types 🚀
Stars: ✭ 31 (-44.64%)
Mutual labels:  xcode, swift-package-manager
Bfkit Swift
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.
Stars: ✭ 963 (+1619.64%)
Mutual labels:  xcode, swift-package-manager
Mastermind
Man in the middle testing
Stars: ✭ 341 (+508.93%)
Mutual labels:  test, mock
Mockito
HTTP mocking for Rust!
Stars: ✭ 335 (+498.21%)
Mutual labels:  test, mock
Mimic
Seamless client side mocking
Stars: ✭ 380 (+578.57%)
Mutual labels:  test, mock
Swift5 Module Template
An opinionated starting point for awesome, reusable Swift 5 modules
Stars: ✭ 331 (+491.07%)
Mutual labels:  xcode, swift-package-manager
Smocker
Smocker is a simple and efficient HTTP mock server and proxy.
Stars: ✭ 465 (+730.36%)
Mutual labels:  test, mock
Imagecoordinatespace
UICoordinateSpace for UIImageView image
Stars: ✭ 42 (-25%)
Mutual labels:  xcode, playground
Containercontroller
UI Component. This is a copy swipe-panel from app: Apple Maps, Stocks. Swift version
Stars: ✭ 273 (+387.5%)
Mutual labels:  xcode, swift-package-manager
Bluepill
Bluepill is a reliable iOS testing tool that runs UI tests using multiple simulators on a single machine
Stars: ✭ 3,080 (+5400%)
Mutual labels:  xcode, test
Swiftmockgeneratorforxcode
An Xcode extension (plugin) to generate Swift test doubles automatically.
Stars: ✭ 522 (+832.14%)
Mutual labels:  xcode, mock

MockSwift

Welcome to MockSwift!

Release Build Status Codacy Badge Codacy Badge documentation Swift Package Manager compatible Swift license MIT

MockSwift allows you to write mocks and make better tests. Because MockSwift is an open source library 100% written in Swift, it is AVAILABLE ON ALL PLATFORMS.
Initially MockSwift is inspired by Mockito.

Table of Contents

Features

Actually MockSwift supports:

  • Mocking
    • [x] protocol (methods, properties, subscripts)
    • [ ] class (v2)
    • [ ] struct (v2)
    • [ ] enum (v2)
  • Call verification
    • [x] protocol (methods, properties, subscripts)
    • [ ] class (v2)
    • [ ] struct (v2)
    • [ ] enum (v2)
  • [x] Generics
  • [x] Parameters matching
  • [x] Default values for Types

CHANGELOG

You can see all changes and new features here.

Installation

Swift Package Manager

MockSwift has been designed to work with Swift Package Manager.

// swift-tools-version:5.3

import PackageDescription

let package = Package(
  name: "MyProject",
  dependencies: [
    .package(url: "https://github.com/leoture/MockSwift.git", from: "0.7.0")
  ],
  targets: [
    .testTarget(name: "MyProjectTests", dependencies: ["MockSwift"])
  ]
)

Usage

Suppose that you have a UserService protocol.

struct User: Equatable {
  let identifier: String
  let name: String
}

protocol UserService {
  func fetch(identifier: String) -> User
}

And you want to test this UserCore class.

class UserCore {
  private let service: UserService

  init(_ service: UserService) {
    self.service = service
  }

  func fetchCurrentUser() -> User {
    service.fetch(identifier: "current")
  }
}

Make better tests

Now, with MockSwift, you can use a mocked UserService in your tests with the @Mock annotation.

@Mock private var service: UserService

// equivalent to

private var service: UserService = Mock()

And easly configure it to fully test UseCore.

class UserCoreTests: XCTestCase {

  private var core: UserCore!
  @Mock private var service: UserService

  override func setUp() {
    core = UserCore(service)
  }

  func test_fetchCurrentUser() {
    // Given
    let expectedUser = User(identifier: "current", name: "John")

    given(service).fetch(identifier: .any()).willReturn(expectedUser)

    // When
    let user = core.fetchCurrentUser()

    // Then
    then(service).fetch(identifier: .any()).called()
    XCTAssertEqual(user, expectedUser)
  }
}

Given

given() enables you to define behaviours.
example:

given(service).fetch(identifier: .any()).willReturn(expectedUser)

// equivalent to

given(service) {
  $0.fetch(identifier: .any()).willReturn(expectedUser)
}
given(service) {
  $0.fetch(identifier: "current")
    .willReturn(expectedUser, expectedUser1, expectedUser2)

  $0.fetch(identifier: .match(when: \.isEmpty))
    .will { (params) -> User in
            // do something else
            return expectedUser
          }
}

you can also define behaviours when you instantiate the mock.

@Mock({
  $0.fetch(identifier: .any()).willReturn(expectedUser)
})
private var service: UserService

Then

then() enables you to verify calls.
example:

then(service).fetch(identifier: .any()).called()

// equivalent to

then(service) {
  $0.fetch(identifier: .any()).called()
}
then(service) {
  $0.fetch(identifier: "current").called(times: >=2)

  $0.fetch(identifier: =="").called(times: 0)
}

Stubs

In MockSwift, stubs are default values that are returned when no behaviours has been found.

Global Stubs

You can define a global stub for any type. It will concern all mocks you will use in every tests.

extension User: GlobalStub {
  static func stub() -> User {
    User(identifier: "id", name: "John")
  }
}

Local Stubs

You can also define a stub localy for any type. It will concern only the current mock.

@Mock(localStubs: [
      User.self => User(identifier: "id", name: "John")
])
private var service: UserService

Strategy

The default strategy is to find behaviour defined with given(). If no behaviour is found, it will return a local stub. If no local stub is found, it will return a global stub.

@Mock private var service: UserService

// equivalent to

@Mock(strategy: .default)
private var service: UserService

// equivalent to

@Mock(strategy: [.given, .localStubs, .globalStubs])
private var service: UserService

You can change the order of the strategy list or remove items as you want.

Write mocks

Automatically

MockSwift provides a stencil template for sourcery. You can use the AutoMockable annotation to generate code.

// sourcery: AutoMockable
protocol UserService {
  func fetch(identifier: String) -> User
}

To generate code at every build, you can add a build phase before Compile Sources.

sourcery \
--sources MyLibrary \
--templates MyLibraryTests/path/to/MockSwift.stencil \
--output MyLibraryTests/path/to/GeneratedMocks.swift \
--args module=MyLibrary

Manually

To enable MockSwift for UserService type, you have to extend Mock.

extension Mock: UserService where WrappedType == UserService {
  public func fetch(identifier: String) -> User {
    mocked(identifier)
  }
}

To allow behaviour definition through given() method, you have to extend Given.

extension Given where WrappedType == UserService {
  public func fetch(identifier: Predicate<String>) -> Mockable<User> {
    mockable(identifier)
  }
  public func fetch(identifier: String) -> Mockable<User> {
    mockable(identifier)
  }
}

To allow call verification through then() method, you have to extend Then.

extension Then where WrappedType == UserService {
  public func fetch(identifier: Predicate<String>) -> Verifiable<User> {
    verifiable(identifier)
  }
  public func fetch(identifier: String) -> Verifiable<User> {
    verifiable(identifier)
  }
}

Documentation

If you need more details about the API, you can check out our API documentation or our GitBook.

Playgrounds

This project contains playgrounds that can help you experiment MockSwift .
To use playgrounds:

  • open MockSwift.xcworkspace
  • build the MockSwiftPlayground scheme.

Contribution

Would you like to contribute to MockSwift? Please read our contributing guidelines and code of conduct.

License

MockSwift is released under the MIT license. See LICENSE for details.

Credits

Thanks to JetBrains

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