How to use testStubAsyncClosureMethod method of StubbingAsyncTests class

Best Mockingbird code snippet using StubbingAsyncTests.testStubAsyncClosureMethod

Run Mockingbird automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

StubbingAsyncTests.swift

Source: StubbingAsyncTests.swift Github

copy
1import Foundation
2import Mockingbird
3@testable import MockingbirdTestsHost
4import XCTest
5
6#if swift(>=5.5.2)
7@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
8extension XCTest {
9  func XCTAssertThrowsAsyncError<T: Sendable>(
10    _ expression: @autoclosure () async throws -> T,
11    _ message: @autoclosure () -> String = "XCTAssertThrowsAsyncError failed: did not throw an error",
12    file: StaticString = #filePath,
13    line: UInt = #line,
14    _ errorHandler: (_ error: Error) -> Void = { _ in }
15  ) async {
16    do {
17      _ = try await expression()
18      XCTFail(message(), file: file, line: line)
19    } catch {
20      errorHandler(error)
21    }
22  }
23}
24
25@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
26class StubbingAsyncTests: BaseTestCase {
27  
28  struct FakeError: Error {}
29  
30  var asyncProtocol: AsyncProtocolMock!
31  var asyncProtocolInstance: AsyncProtocol { asyncProtocol }
32  
33  override func setUp() {
34    asyncProtocol = mock(AsyncProtocol.self)
35  }
36  
37  func testStubAsyncMethodVoid() async {
38    given(await asyncProtocol.asyncMethodVoid()).willReturn()
39    await asyncProtocolInstance.asyncMethodVoid()
40    verify(await asyncProtocol.asyncMethodVoid()).wasCalled()
41  }
42  
43  func testStubAsyncMethod_returnsValue() async {
44    given(await asyncProtocol.asyncMethod()) ~> true
45    
46    let result: Bool = await asyncProtocolInstance.asyncMethod()
47
48    XCTAssertEqual(result, true)
49    verify(await asyncProtocol.asyncMethod()).wasCalled()
50  }
51  
52  func testStubAsyncMethodWithParameter_returnsValue() async {
53    given(await asyncProtocol.asyncMethod(parameter: any())) ~> 2
54    
55    let result: Int = await asyncProtocolInstance.asyncMethod(parameter: "parameter")
56
57    XCTAssertEqual(result, 2)
58    verify(await asyncProtocol.asyncMethod(parameter: "parameter")).wasCalled()
59  }
60  
61  func testStubAsyncThrowingMethod_returnsValue() async throws {
62    given(await asyncProtocol.asyncThrowingMethod()) ~> 1
63    
64    let result: Int = try await asyncProtocolInstance.asyncThrowingMethod()
65
66    XCTAssertEqual(result, 1)
67    verify(await asyncProtocol.asyncThrowingMethod()).wasCalled()
68  }
69  
70  func testStubAsyncThrowingMethod_throwsError() async throws {
71    given(await asyncProtocol.asyncThrowingMethod()) ~> { () throws -> Int in throw FakeError() }
72    await XCTAssertThrowsAsyncError(try await asyncProtocolInstance.asyncThrowingMethod())
73    verify(await asyncProtocol.asyncThrowingMethod()).wasCalled()
74  }
75  
76  func testStubAsyncClosureMethod() async throws {
77    given(await asyncProtocol.asyncClosureMethod(block: any())).willReturn()
78    await asyncProtocolInstance.asyncClosureMethod(block: { true })
79    verify(await asyncProtocol.asyncClosureMethod(block: any())).wasCalled()
80  }
81  
82  func testStubAsyncClosureThrowingMethod_returnsValue() async throws {
83    given(await asyncProtocol.asyncClosureThrowingMethod(block: any())) ~> true
84    
85    let result: Bool = try await asyncProtocolInstance.asyncClosureThrowingMethod(block: { false })
86
87    XCTAssertTrue(result)
88    verify(await asyncProtocol.asyncClosureThrowingMethod(block: any())).wasCalled()
89  }
90  
91  func testStubAsyncClosureThrowingMethod_throwsError() async throws {
92    given(await asyncProtocol.asyncClosureThrowingMethod(block: any())) ~> { _ in throw FakeError() }
93    await XCTAssertThrowsAsyncError(try await asyncProtocolInstance.asyncClosureThrowingMethod(block: { true }))
94    verify(await asyncProtocol.asyncClosureThrowingMethod(block: any())).wasCalled()
95  }
96  
97  func testStubAsyncClosureThrowingMethod_returnsValueFromBlock() async throws {
98    given(await asyncProtocol.asyncClosureThrowingMethod(block: any())) ~> { try await $0() }
99    
100    let result: Bool = try await asyncProtocolInstance.asyncClosureThrowingMethod(block: { true })
101
102    XCTAssertTrue(result)
103    verify(await asyncProtocol.asyncClosureThrowingMethod(block: any())).wasCalled()
104  }
105  
106  func testStubAsyncClosureThrowingMethod_throwsErrorFromBlock() async throws {
107    given(await asyncProtocol.asyncClosureThrowingMethod(block: any())) ~> { try await $0() }
108    await XCTAssertThrowsAsyncError(try await asyncProtocolInstance.asyncClosureThrowingMethod(block: { throw FakeError() }))
109    verify(await asyncProtocol.asyncClosureThrowingMethod(block: any())).wasCalled()
110  }
111  
112}
113#endif
114
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)