Popularity
1.1
Growing
Activity
7.5
Growing
38
4
1

Programming language: Swift
Tags: Testing     Mock    
Latest version: v0.5.0

MockSwift alternatives and similar libraries

Based on the "Mock" category

Do you think we are missing an alternative of MockSwift or a related project?

Add another 'Mock' Library

README


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


*Note that all licence references and agreements mentioned in the MockSwift README section above are relevant to that project's source code only.