How to use testClearInvocations method of ObjectiveCTests class

Best Mockingbird code snippet using ObjectiveCTests.testClearInvocations

Run Mockingbird automation tests on LambdaTest cloud grid

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

ObjectiveCTests.swift

Source: ObjectiveCTests.swift Github

copy
1//
2//  ObjectiveCTests.swift
3//  MockingbirdTests
4//
5//  Created by typealias on 7/28/21.
6//
7
8import CoreBluetooth
9import Mockingbird
10@testable import MockingbirdTestsHost
11import XCTest
12
13class ObjectiveCTests: BaseTestCase {
14  
15  var centralManagerMock: CBCentralManager!
16  var delegateMock: CBCentralManagerDelegate!
17  var peripheralMock: CBPeripheral!
18  
19  override func setUpWithError() throws {
20    self.centralManagerMock = mock(CBCentralManager.self)
21    self.delegateMock = mock(CBCentralManagerDelegate.self)
22    self.peripheralMock = mock(CBPeripheral.self)
23  }
24  
25  func testReferenceTypePropertyGetterEquatable() throws {
26    let uid = "BA6C41BD-E803-4527-A91A-9951ADC57CBF"
27    given(peripheralMock.identifier).willReturn(UUID(uuidString: uid))
28    XCTAssertEqual(peripheralMock.identifier, UUID(uuidString: uid))
29    verify(peripheralMock.identifier).wasCalled()
30  }
31  func testReferenceTypePropertyGetterEquatable_stubbingOperator() throws {
32    let uid = "BA6C41BD-E803-4527-A91A-9951ADC57CBF"
33    given(peripheralMock.identifier) ~> UUID(uuidString: uid)
34    XCTAssertEqual(peripheralMock.identifier, UUID(uuidString: uid))
35    verify(peripheralMock.identifier).wasCalled()
36  }
37  
38  func testReferenceTypePropertyGetterIdentical() throws {
39    given(self.centralManagerMock.delegate).willReturn(delegateMock)
40    XCTAssertTrue(centralManagerMock.delegate === delegateMock)
41  }
42  func testReferenceTypePropertyGetterIdentical_stubbingOperator() throws {
43    given(self.centralManagerMock.delegate) ~> self.delegateMock
44    XCTAssertTrue(centralManagerMock.delegate === delegateMock)
45  }
46  
47  func testBridgedTypePropertyGetter() throws {
48    given(peripheralMock.name).willReturn("Ryan")
49    XCTAssertEqual(peripheralMock.name, "Ryan")
50    verify(peripheralMock.name).wasCalled()
51  }
52  func testBridgedTypePropertyGetter_stubbingOperator() throws {
53    given(peripheralMock.name) ~> "Ryan"
54    XCTAssertEqual(peripheralMock.name, "Ryan")
55    verify(peripheralMock.name).wasCalled()
56  }
57
58  func testMethodWithExactMatching() throws {
59    let expectation = XCTestExpectation()
60    given(centralManagerMock.cancelPeripheralConnection(peripheralMock)).will {
61      (peripheral: CBPeripheral) in
62      XCTAssertEqual(peripheral, self.peripheralMock)
63      expectation.fulfill()
64    }
65    centralManagerMock.cancelPeripheralConnection(peripheralMock)
66    verify(self.centralManagerMock.cancelPeripheralConnection(peripheralMock)).wasCalled()
67    wait(for: [expectation], timeout: 2)
68  }
69  func testMethodWithExactMatching_stubbingOperator() throws {
70    let expectation = XCTestExpectation()
71    given(centralManagerMock.cancelPeripheralConnection(peripheralMock)) ~> {
72      (peripheral: CBPeripheral) in
73      XCTAssertEqual(peripheral, self.peripheralMock)
74      expectation.fulfill()
75    }
76    centralManagerMock.cancelPeripheralConnection(peripheralMock)
77    verify(self.centralManagerMock.cancelPeripheralConnection(peripheralMock)).wasCalled()
78    wait(for: [expectation], timeout: 2)
79  }
80  
81  func testMethodWithWildcardMatching() throws {
82    let expectation = XCTestExpectation()
83    given(centralManagerMock.cancelPeripheralConnection(any())).will {
84      (peripheral: CBPeripheral) in
85      XCTAssertEqual(peripheral, self.peripheralMock)
86      expectation.fulfill()
87    }
88    centralManagerMock.cancelPeripheralConnection(peripheralMock)
89    verify(self.centralManagerMock.cancelPeripheralConnection(any())).wasCalled()
90    wait(for: [expectation], timeout: 2)
91  }
92  func testMethodWithWildcardMatching_stubbingOperator() throws {
93    let expectation = XCTestExpectation()
94    given(centralManagerMock.cancelPeripheralConnection(any())) ~> {
95      (peripheral: CBPeripheral) in
96      XCTAssertEqual(peripheral, self.peripheralMock)
97      expectation.fulfill()
98    }
99    centralManagerMock.cancelPeripheralConnection(peripheralMock)
100    verify(self.centralManagerMock.cancelPeripheralConnection(any())).wasCalled()
101    wait(for: [expectation], timeout: 2)
102  }
103  
104  func testMethodArgumentCaptor() throws {
105    let uid = "BA6C41BD-E803-4527-A91A-9951ADC57CBF"
106    given(peripheralMock.identifier).willReturn(UUID(uuidString: uid))
107    
108    let expectation = XCTestExpectation()
109    given(centralManagerMock.cancelPeripheralConnection(any())).will {
110      (peripheral: CBPeripheral) in
111      XCTAssertEqual(peripheral, self.peripheralMock)
112      expectation.fulfill()
113    }
114    
115    centralManagerMock.cancelPeripheralConnection(peripheralMock)
116
117    let peripheralCaptor = ArgumentCaptor<CBPeripheral>()
118    verify(centralManagerMock.cancelPeripheralConnection(peripheralCaptor.any())).wasCalled()
119    XCTAssertEqual(peripheralCaptor.value?.identifier, UUID(uuidString: uid))
120    wait(for: [expectation], timeout: 2)
121  }
122  func testMethodArgumentCaptor_stubbingOperator() throws {
123    let uid = "BA6C41BD-E803-4527-A91A-9951ADC57CBF"
124    given(peripheralMock.identifier) ~> UUID(uuidString: uid)
125    
126    let expectation = XCTestExpectation()
127    given(centralManagerMock.cancelPeripheralConnection(any())) ~> {
128      (peripheral: CBPeripheral) in
129      XCTAssertEqual(peripheral, self.peripheralMock)
130      expectation.fulfill()
131    }
132    
133    centralManagerMock.cancelPeripheralConnection(peripheralMock)
134
135    let peripheralCaptor = ArgumentCaptor<CBPeripheral>()
136    verify(centralManagerMock.cancelPeripheralConnection(peripheralCaptor.any())).wasCalled()
137    XCTAssertEqual(peripheralCaptor.value?.identifier, UUID(uuidString: uid))
138    wait(for: [expectation], timeout: 2)
139  }
140  
141  
142  // MARK: - Invocation forwarding
143  
144  func testForwardGetterInvocation() throws {
145    let uid = "BA6C41BD-E803-4527-A91A-9951ADC57CBF"
146    let target = mock(CBPeripheral.self)
147    given(target.identifier).willReturn(UUID(uuidString: uid))
148    
149    given(peripheralMock.identifier).willForward(to: target)
150    XCTAssertEqual(peripheralMock.identifier, UUID(uuidString: uid))
151    
152    verify(peripheralMock.identifier).wasCalled()
153    verify(target.identifier).wasCalled()
154  }
155  func testForwardGetterInvocation_stubbingOperator() throws {
156    let uid = "BA6C41BD-E803-4527-A91A-9951ADC57CBF"
157    let target = mock(CBPeripheral.self)
158    given(target.identifier) ~> UUID(uuidString: uid)
159    
160    given(peripheralMock.identifier) ~> forward(to: target)
161    XCTAssertEqual(peripheralMock.identifier, UUID(uuidString: uid))
162    
163    verify(peripheralMock.identifier).wasCalled()
164    verify(target.identifier).wasCalled()
165  }
166  
167  func testForwardMethodInvocation() throws {
168    let expectation = XCTestExpectation()
169    let target = mock(CBCentralManager.self)
170    given(target.cancelPeripheralConnection(any())).will { expectation.fulfill() }
171    
172    given(centralManagerMock.cancelPeripheralConnection(any())).willForward(to: target)
173    centralManagerMock.cancelPeripheralConnection(peripheralMock)
174    
175    verify(centralManagerMock.cancelPeripheralConnection(any())).wasCalled()
176    verify(target.cancelPeripheralConnection(any())).wasCalled()
177    wait(for: [expectation], timeout: 2)
178  }
179  func testForwardMethodInvocation_stubbingOperator() throws {
180    let expectation = XCTestExpectation()
181    let target = mock(CBCentralManager.self)
182    given(target.cancelPeripheralConnection(any())) ~> { expectation.fulfill() }
183    
184    given(centralManagerMock.cancelPeripheralConnection(any())) ~> forward(to: target)
185    centralManagerMock.cancelPeripheralConnection(peripheralMock)
186    
187    verify(centralManagerMock.cancelPeripheralConnection(any())).wasCalled()
188    verify(target.cancelPeripheralConnection(any())).wasCalled()
189    wait(for: [expectation], timeout: 2)
190  }
191  
192  
193  // MARK: - Mock resetting
194  
195  func testResetEntireContext() throws {
196    centralManagerMock.cancelPeripheralConnection(peripheralMock)
197    verify(centralManagerMock.cancelPeripheralConnection(peripheralMock)).wasCalled()
198    reset(centralManagerMock)
199    verify(centralManagerMock.cancelPeripheralConnection(peripheralMock)).wasNeverCalled()
200  }
201  
202  func testClearInvocations() throws {
203    centralManagerMock.cancelPeripheralConnection(peripheralMock)
204    verify(centralManagerMock.cancelPeripheralConnection(peripheralMock)).wasCalled()
205    clearInvocations(on: centralManagerMock)
206    verify(centralManagerMock.cancelPeripheralConnection(peripheralMock)).wasNeverCalled()
207  }
208  
209  func testClearStubs() throws {
210    given(centralManagerMock.isScanning).willReturn(true)
211    XCTAssertTrue(centralManagerMock.isScanning)
212    clearStubs(on: centralManagerMock)
213    shouldFail {
214      _ = self.centralManagerMock.isScanning
215    }
216  }
217}
218
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)