How to use recordError method of InvocationRecorder class

Best Mockingbird code snippet using InvocationRecorder.recordError

Run Mockingbird automation tests on LambdaTest cloud grid

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

InvocationRecorder.swift

Source: InvocationRecorder.swift Github

copy
1//
2//  InvocationRecorder.swift
3//  MockingbirdFramework
4//
5//  Created by typealias on 7/18/21.
6//
7
8import Foundation
9
10struct InvocationRecord {
11  let invocation: Invocation
12  let context: Context
13}
14
15/// Records invocations for stubbing and verification.
16@objc(MKBInvocationRecorder) public class InvocationRecorder: NSObject {
17  
18  /// Used to attribute declarations to stubbing and verification calls in tests.
19  @objc(MKBInvocationRecorderMode) public enum Mode: UInt {
20    case none = 0
21    case stubbing
22    case verifying
23  }
24  
25  enum Constants {
26    static let recorderKey = DispatchSpecificKey<InvocationRecorder>()
27    static let resultSentinel = NSException(
28      name: NSExceptionName(rawValue: "co.bird.mockingbird.InvocationRecorder.result"),
29      reason: nil,
30      userInfo: nil)
31  }
32  
33  enum Result {
34    case value(InvocationRecord)
35    case error(TestFailure)
36  }
37  private(set) var result: Result? {
38    didSet {
39      MKBThrowException(Constants.resultSentinel)
40    }
41  }
42  
43  private(set) var facadeValues = [Int: Any?]()
44  private(set) var argumentIndex: Int?
45  
46  /// Gracefully handle invocations with only a single argument by deferring errors until later.
47  private(set) var unindexedFacadeValues = [(value: Any?, pendingError: TestFailure)]()
48  
49  @objc public let mode: Mode
50  private static let sharedQueue = DispatchQueue(label: "co.bird.mockingbird.InvocationRecorder")
51  
52  init(mode: Mode) {
53    self.mode = mode
54  }
55  
56  func startRecording(block: () -> Void) -> Self {
57    Self.sharedQueue.sync {
58      _ = MKBTryBlock {
59        Self.sharedQueue.setSpecific(key: Constants.recorderKey, value: self)
60        block()
61      }
62      Self.sharedQueue.setSpecific(key: Constants.recorderKey, value: nil)
63    }
64    return self
65  }
66  
67  func recordInvocation(_ invocation: Invocation, context: Context) {
68    result = .value(InvocationRecord(invocation: invocation, context: context))
69  }
70  
71  @objc public func recordInvocation(_ invocation: ObjCInvocation, context: Context) {
72    recordInvocation(invocation as Invocation, context: context)
73  }
74  
75  func recordError(_ error: TestFailure) -> Never {
76    result = .error(error)
77    fatalError("This should never run")
78  }
79  
80  func recordArgumentIndex(_ index: Int) {
81    argumentIndex = index
82  }
83  
84  func recordFacadeValue(_ facadeValue: Any?, at index: Int) {
85    facadeValues[index] = facadeValue
86    argumentIndex = nil
87  }
88  
89  func recordUnindexedFacadeValue(_ facadeValue: Any?, error: TestFailure) {
90    unindexedFacadeValues.append((facadeValue, error))
91  }
92  
93  @objc public func getFacadeValue(at argumentIndex: Int, argumentsCount: Int) -> Any? {
94    // Indexes can only be inferred when the argument matching is homogenous.
95    // For example, arguments [any(), any()] and [1, 2] could be inferred, but [1, any()] could not.
96    if let indexedFacadeValue = facadeValues[argumentIndex] {
97      return indexedFacadeValue
98    } else if let unindexedFacadeValue = unindexedFacadeValues.get(argumentIndex)?.value,
99              argumentsCount == unindexedFacadeValues.count {
100      return unindexedFacadeValue
101    } else if let error = unindexedFacadeValues.last?.pendingError {
102      recordError(error)
103    } else {
104      return nil // Shouldn't be possible to reach this branch.
105    }
106  }
107  
108  // MARK: DispatchQueue utils
109  
110  /// The global invocation recorder instance.
111  @objc public static var sharedRecorder: InvocationRecorder? {
112    return DispatchQueue.getSpecific(key: Constants.recorderKey)
113  }
114}
115
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

Trigger recordError code on LambdaTest Cloud Grid

Execute automation tests with recordError on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)