Swiftpack.co - Package - leoture/MockSwift

MockSwift Welcome to MockSwift!

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

Github

link
Stars: 3
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

v0.1.0 - Aug 22, 2019

2019-08-22

  • Initial release