Swiftpack.co - Package - leoture/MockSwift

MockSwift

Welcome to MockSwift!

Release Build Status Maintainability Test Coverage documentation Swift Package Manager compatible Swift license MIT

MockSwift is a Mock library for Swift inspired by Mockito.

⚠️ This README is out of date. Full documentation coming soon.

Table of Contents

Installation

Swift Package Manager

MockSwift is thinking to work with Swift Package Manager.

// swift-tools-version:5.1

import PackageDescription

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

Usage

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

Suppose that you have a UserService protocol.

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

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

Basic usage

Now, you can use UserService into your tests with the @Mock annotation.

class MyTests: XCTestCase {
  @Mock private var service: UserService

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

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

    // When
    let user = service.fetch(identifier: "id")

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

GlobalStub

You can define a global stub for any type.
This value will be returned for any mocked method returning this type, only if no behaviour has been defined.

extension User: GlobalStub {
  static func stub() -> User {
    User(identifier: "id", name: "John")
  }
}
func test_fetch_withDefault() {
    // Given
    let expectedUser = User(identifier: "id", name: "John")

    // When
    let user = service.fetch(identifier: "id")

    // Then
    XCTAssertEqual(user, expectedUser)
}

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

Github

link
Stars: 36

Dependencies

Used By

Total: 0

Releases

v0.4.0 - 2019-11-12 - 2019-11-12 15:05:12

Added

Changed

  • Template sourcery only generates Mock with AutoMockable annotation #43 by Jordhan Leoture
  • The Input type of Predicate.match and Predicate.any can be disambiguate #58 by Jordhan Leoture

Fixed

v0.3.0 - 2019-10-16 - 2019-10-17 08:17:22

Added

  • Add Predicate .match(description:keyPath:) #38
  • Add willThrow #37
  • Add willReturn with a List #36
  • Add Predicates with Comparables #28
  • Add Predicates .isTrue() & .isFalse() #33
  • Add Predicate .not() #33
  • Add Predicate .isNil() #33

Changed

  • mocked() with optionals #32

V0.2.0 - 2019-09-29 - 2019-09-29 11:26:43

Added

  • New Logo
  • Template sourcery #20

Changed

  • Given with wrapped type #18
  • Then with wrapped type #21
  • Given & Then with completion block #22

- 2019-08-22 13:12:49

2019-08-22

  • Initial release