Swiftpack.co - Package - leoture/MockSwift

MockSwift

Welcome to MockSwift!

Release Build Status codecov documentation Swift Package Manager compatible Swift license MIT

MockSwift is a Mock library written in Swift for Swift.

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)
  }
}

MockDefault

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

extension User: MockDefault {
  static func `default`() -> 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: 29
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

v0.4.0 - Nov 12, 2019

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 - Oct 17, 2019

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 - Sep 29, 2019

Added

  • New Logo
  • Template sourcery #20

Changed

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

v0.1.0 - Aug 22, 2019

2019-08-22

  • Initial release