### MockSwift Quick Look Test Example Source: https://github.com/leoture/mockswift/blob/master/README.md This example demonstrates the basic workflow of using MockSwift within an XCTestCase. It shows how to declare a mock using @Mock, set up behaviors with given(), call the system under test, and verify interactions with then(). ```Swift class AwesomeTests: XCTestCase { private var printer: Printer! @Mock private var userService: UserService override func setUp() { printer = Printer(userService) } func test_sayHello() { // Given given(userService).fetchUserName(of: "you").willReturn("my friend") given(userService).isConnected.get.willReturn(true) given(userService)[cache: .any()].set(.any()).willDoNothing() // When let message = printer.sayHello(to: "you", from: "me") // Then then(userService).fetchUserName(of: .any()).called() then(userService).isConnected.get.called(times: 1) then(userService)[cache: "you"].set("my friend").calledOnce() XCTAssertEqual(message, "me: Hello my friend") } } ``` -------------------------------- ### Sourcery Build Phase Configuration for MockSwift (Shell) Source: https://github.com/leoture/mockswift/blob/master/README.md Provides an example command-line configuration for integrating Sourcery into a build phase to automatically generate MockSwift code from annotated sources. ```Shell sourcery \ --sources MyLibrary \ --templates MyLibraryTests/path/to/MockSwift.stencil \ --output MyLibraryTests/path/to/GeneratedMocks.swift \ --args module=MyLibrary ``` -------------------------------- ### Define UserService Protocol and User Struct Source: https://github.com/leoture/mockswift/blob/master/README.md This snippet defines a simple Swift struct `User` and a protocol `UserService` which will be used as examples to demonstrate how to mock dependencies in tests. ```Swift struct User: Equatable { let identifier: String let name: String } protocol UserService { func fetch(identifier: String) -> User } ``` -------------------------------- ### Test UserCore with Mocked UserService Source: https://github.com/leoture/mockswift/blob/master/README.md This example demonstrates how to write a unit test for the `UserCore` class using a mocked `UserService`. It shows setting up mock behavior with `given()` and verifying method calls with `then()`. ```Swift 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) } } ``` -------------------------------- ### Define Multiple Mock Behaviors and Custom Actions Source: https://github.com/leoture/mockswift/blob/master/README.md This example shows how to define multiple behaviors for a mock within a single `given()` closure. It also demonstrates using `.will()` to execute custom code and return a value based on the call parameters. ```Swift 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 } } ``` -------------------------------- ### Define UserCore Class Depending on UserService Source: https://github.com/leoture/mockswift/blob/master/README.md This snippet defines a `UserCore` class that depends on the `UserService` protocol. This class will be the system under test in subsequent examples, showing how to inject and use a mocked service. ```Swift class UserCore { private let service: UserService init(_ service: UserService) { self.service = service } func fetchCurrentUser() -> User { service.fetch(identifier: "current") } } ``` -------------------------------- ### Build and Test Swift Project Source: https://github.com/leoture/mockswift/blob/master/CONTRIBUTING.md These commands are used to build and test the MockSwift project locally using the Swift Package Manager. ```Shell swift build ``` ```Shell swift test ``` -------------------------------- ### Enabling Automatic Mock Generation with Sourcery (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Shows how to use the // sourcery: AutoMockable annotation on a protocol to mark it for automatic mock code generation using Sourcery and the MockSwift template. ```Swift // sourcery: AutoMockable protocol UserService { func fetch(identifier: String) -> User } ``` -------------------------------- ### Verifying Mock Calls with Predicates and Times in Swift Source: https://github.com/leoture/mockswift/blob/master/README.md Shows how to use then() with predicates (e.g., .any(), ==) and specify the expected number of calls using the times parameter. ```Swift then(service) { $0.fetch(identifier: "current").called(times: >=2) $0.fetch(identifier: == "").called(times: 0) } ``` -------------------------------- ### Verifying Mock Calls with then() in Swift Source: https://github.com/leoture/mockswift/blob/master/README.md Demonstrates the basic usage of then() to verify that a specific method was called on a mock object. Shows both the chained and block syntax. ```Swift then(service).fetch(identifier: .any()).called() // equivalent to then(service) { $0.fetch(identifier: .any()).called() } ``` -------------------------------- ### Run Tests on Linux with Docker Source: https://github.com/leoture/mockswift/blob/master/CONTRIBUTING.md This command is used to run the project's tests within a Linux environment using Docker Compose, which is necessary for testing on non-macOS platforms. ```Shell docker-compose up ``` -------------------------------- ### Define Mock Behavior with given() Source: https://github.com/leoture/mockswift/blob/master/README.md This snippet illustrates two syntax options for defining mock behaviors using the `given()` function: a single-line chain or a closure-based syntax for better readability with multiple behaviors. ```Swift given(service).fetch(identifier: .any()).willReturn(expectedUser) // equivalent to given(service) { $0.fetch(identifier: .any()).willReturn(expectedUser) } ``` -------------------------------- ### Define Initial Mock Behavior with @Mock Source: https://github.com/leoture/mockswift/blob/master/README.md This snippet shows how to define initial behaviors for a mock directly when declaring it using the `@Mock` property wrapper by providing a closure. ```Swift @Mock({ $0.fetch(identifier: .any()).willReturn(expectedUser) }) private var service: UserService ``` -------------------------------- ### Configuring Mock Resolution Strategy in MockSwift (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Explains the default strategy for resolving mock behavior and demonstrates how to explicitly set or customize the strategy using the @Mock attribute. ```Swift @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 ``` -------------------------------- ### Add MockSwift with Swift Package Manager Source: https://github.com/leoture/mockswift/blob/master/README.md This snippet shows how to integrate MockSwift into your Swift project using Swift Package Manager by adding it as a dependency in your Package.swift file, typically for test targets. ```Swift // swift-tools-version:5.3 import PackageDescription let package = Package( name: "MyProject", dependencies: [ .package(url: "https://github.com/leoture/MockSwift.git", from: "1.0.0") ], targets: [ .testTarget(name: "MyProjectTests", dependencies: ["MockSwift"]) ] ) ``` -------------------------------- ### Defining Local Stubs in MockSwift (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Shows how to define stubs that are local to a specific mock instance using the @Mock attribute and the localStubs parameter. ```Swift @Mock(localStubs: [ User.self => User(identifier: "id", name: "John") ]) private var service: UserService ``` -------------------------------- ### Manually Implementing Given Extension in MockSwift (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Shows how to manually extend Given for a specific type to allow defining mock behaviors using the given() method, implementing methods to call mockable(). ```Swift extension Given where WrappedType == UserService { public func fetch(identifier: Predicate) -> Mockable { mockable(identifier) } public func fetch(identifier: String) -> Mockable { mockable(identifier) } } ``` -------------------------------- ### Defining Global Stubs in MockSwift (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Explains how to define a global stub for a specific type by extending the type and conforming to the GlobalStub protocol, providing a default stub() implementation. ```Swift extension User: GlobalStub { static func stub() -> User { User(identifier: "id", name: "John") } } ``` -------------------------------- ### Manually Implementing Then Extension in MockSwift (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Demonstrates how to manually extend Then for a specific type to allow verifying mock calls using the then() method, implementing methods to call verifiable(). ```Swift extension Then where WrappedType == UserService { public func fetch(identifier: Predicate) -> Verifiable { verifiable(identifier) } public func fetch(identifier: String) -> Verifiable { verifiable(identifier) } } ``` -------------------------------- ### Verifying Mock Call Order with then() in Swift Source: https://github.com/leoture/mockswift/blob/master/README.md Illustrates how to verify the order in which mock methods were called by using the after parameter with a previous assertion. ```Swift let assertion = then(service).fetch(identifier: "current").called(times: >=2) then(service).fetch(identifier: == "").called(times: 1, after: assertion) ``` -------------------------------- ### Manually Implementing Mock Extension in MockSwift (Swift) Source: https://github.com/leoture/mockswift/blob/master/README.md Explains how to manually enable MockSwift functionality for a specific type by extending Mock and implementing the protocol methods to call mocked(). ```Swift extension Mock: UserService where WrappedType == UserService { public func fetch(identifier: String) -> User { mocked(identifier) } } ``` -------------------------------- ### Declare Mock with @Mock Annotation Source: https://github.com/leoture/mockswift/blob/master/README.md This snippet shows two equivalent ways to declare a mock object using MockSwift: using the `@Mock` property wrapper or explicitly initializing a `Mock()` instance. ```Swift @Mock private var service: UserService // equivalent to private var service: UserService = Mock() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.