How to use testOptionalPropertyGetterLocalForwarding method of ObjCProtocolFake class

Best Mockingbird code snippet using ObjCProtocolFake.testOptionalPropertyGetterLocalForwarding

Run Mockingbird automation tests on LambdaTest cloud grid

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

ObjectiveCProtocolTests.swift

Source: ObjectiveCProtocolTests.swift Github

copy
1import Mockingbird
2@testable import MockingbirdTestsHost
3import XCTest
4
5class ObjectiveCProtocolTests: BaseTestCase {
6  
7  var protocolMock: ObjCProtocolMock!
8  var protocolInstance: ObjCProtocol { protocolMock }
9  
10  var protocolFake = ObjCProtocolFake()
11  
12  class ObjCProtocolFake: Foundation.NSObject, ObjCProtocol {
13    func trivial() {}
14    func parameterizedReturning(param: String) -> Bool { true }
15    
16    var property: Bool { true }
17    var readwriteProperty: Bool = true
18    
19    func optionalTrivial() {}
20    func optionalParameterizedReturning(param: String) -> Bool { true }
21    
22    var optionalProperty: Bool { true }
23    var optionalReadwriteProperty: Bool = true
24    
25    subscript(param: Int) -> Bool {
26      get { true }
27      set {}
28    }
29  }
30  
31  override func setUpWithError() throws {
32    self.protocolMock = mock(ObjCProtocol.self).initialize()
33  }
34  
35  
36  // MARK: - Resetting
37  
38  func testResetMock() {
39    protocolInstance.trivial()
40    reset(protocolMock)
41    verify(protocolMock.trivial()).wasNeverCalled()
42  }
43  
44  func testClearStubs() {
45    given(protocolInstance.parameterizedReturning(param: any())).willReturn(true)
46    clearStubs(on: protocolMock)
47    shouldFail {
48      _ = protocolInstance.parameterizedReturning(param: "foobar")
49    }
50  }
51  
52  func testClearInvocations() {
53    protocolInstance.trivial()
54    clearInvocations(on: protocolMock)
55    verify(protocolMock.trivial()).wasNeverCalled()
56  }
57  
58  
59  // MARK: - Concrete stubs
60  
61  func testTrivial() {
62    given(protocolMock.trivial()).willReturn()
63    protocolInstance.trivial()
64    verify(protocolMock.trivial()).wasCalled()
65  }
66  func testOptionalTrivial() {
67    given(protocolMock.optionalTrivial()).willReturn()
68    protocolInstance.optionalTrivial?()
69    verify(protocolMock.optionalTrivial()).wasCalled()
70  }
71  
72  func testParameterized() {
73    given(protocolMock.parameterizedReturning(param: any())).willReturn(true)
74    XCTAssertTrue(protocolInstance.parameterizedReturning(param: "foobar"))
75    verify(protocolMock.parameterizedReturning(param: any())).wasCalled()
76  }
77  func testOptionalParameterized() {
78    given(protocolMock.optionalParameterizedReturning(param: any())).willReturn(true)
79    XCTAssertTrue(protocolInstance.optionalParameterizedReturning?(param: "foobar") ?? false)
80    verify(protocolMock.optionalParameterizedReturning(param: any())).wasCalled()
81  }
82  
83  func testPropertyGetter() {
84    given(protocolMock.property).willReturn(true)
85    XCTAssertTrue(protocolInstance.property)
86    verify(protocolMock.property).wasCalled()
87  }
88  func testOptionalPropertyGetter() {
89    given(protocolMock.optionalProperty).willReturn(true)
90    XCTAssertTrue(protocolInstance.optionalProperty ?? false)
91    verify(protocolMock.optionalProperty).wasCalled()
92  }
93  
94  func testPropertySetter() {
95    let expectation = expectation(description: "setter was called")
96    given(protocolMock.readwriteProperty = any()).will { (_: Bool) in expectation.fulfill() }
97    protocolInstance.readwriteProperty = true
98    verify(protocolMock.readwriteProperty = any()).wasCalled()
99    waitForExpectations(timeout: 1)
100  }
101  
102  func testOptionalSubscriptGetter() {
103    given(protocolMock[any()]).willReturn(true)
104    XCTAssertTrue(protocolInstance[1] ?? false)
105    verify(protocolMock[any()]).wasCalled()
106  }
107  
108  
109  // MARK: - Object partial mock
110  
111  func testTrivialGlobalForwarding() {
112    protocolMock.forwardCalls(to: protocolFake)
113    protocolInstance.trivial()
114    verify(protocolMock.trivial()).wasCalled()
115  }
116  func testTrivialLocalForwarding() {
117    given(protocolMock.trivial()).willForward(to: protocolFake)
118    protocolInstance.trivial()
119    verify(protocolMock.trivial()).wasCalled()
120  }
121  
122  func testParameterizedGlobalForwarding() {
123    protocolMock.forwardCalls(to: protocolFake)
124    XCTAssertTrue(protocolInstance.parameterizedReturning(param: "foobar"))
125    verify(protocolMock.parameterizedReturning(param: any())).wasCalled()
126  }
127  func testParameterizedLocalForwarding() {
128    given(protocolMock.parameterizedReturning(param: any())).willForward(to: protocolFake)
129    XCTAssertTrue(protocolInstance.parameterizedReturning(param: "foobar"))
130    verify(protocolMock.parameterizedReturning(param: any())).wasCalled()
131  }
132  
133  func testPropertyGetterGlobalForwarding() {
134    protocolMock.forwardCalls(to: protocolFake)
135    XCTAssertTrue(protocolInstance.property)
136    verify(protocolMock.property).wasCalled()
137  }
138  func testPropertyGetterLocalForwarding() {
139    given(protocolMock.property).willForward(to: protocolFake)
140    XCTAssertTrue(protocolInstance.property)
141    verify(protocolMock.property).wasCalled()
142  }
143  
144  func testPropertySetterGlobalForwarding() {
145    protocolMock.forwardCalls(to: protocolFake)
146    let instance = protocolMock as ObjCProtocol
147    instance.readwriteProperty = true
148    verify(protocolMock.readwriteProperty = any()).wasCalled()
149  }
150  func testPropertySetterLocalForwarding() {
151    given(protocolMock.readwriteProperty = any()).willForward(to: protocolFake)
152    let instance = protocolMock as ObjCProtocol
153    instance.readwriteProperty = true
154    verify(protocolMock.readwriteProperty = any()).wasCalled()
155  }
156  
157  // MARK: Optionals
158  
159  func testOptionalTrivialGlobalForwarding() {
160    protocolMock.forwardCalls(to: protocolFake)
161    protocolInstance.optionalTrivial?()
162    verify(protocolMock.optionalTrivial()).wasCalled()
163  }
164  func testOptionalTrivialLocalForwarding() {
165    given(protocolMock.optionalTrivial()).willForward(to: protocolFake)
166    protocolInstance.optionalTrivial?()
167    verify(protocolMock.optionalTrivial()).wasCalled()
168  }
169  
170  func testOptionalParameterizedGlobalForwarding() {
171    protocolMock.forwardCalls(to: protocolFake)
172    XCTAssertTrue(protocolInstance.optionalParameterizedReturning?(param: "foobar") ?? false)
173    verify(protocolMock.optionalParameterizedReturning(param: any())).wasCalled()
174  }
175  func testOptionalParameterizedLocalForwarding() {
176    given(protocolMock.optionalParameterizedReturning(param: any())).willForward(to: protocolFake)
177    XCTAssertTrue(protocolInstance.optionalParameterizedReturning?(param: "foobar") ?? false)
178    verify(protocolMock.optionalParameterizedReturning(param: any())).wasCalled()
179  }
180  
181  func testOptionalPropertyGetterGlobalForwarding() {
182    protocolMock.forwardCalls(to: protocolFake)
183    XCTAssertTrue(protocolInstance.optionalProperty ?? false)
184    verify(protocolMock.optionalProperty).wasCalled()
185  }
186  func testOptionalPropertyGetterLocalForwarding() {
187    given(protocolMock.optionalProperty).willForward(to: protocolFake)
188    XCTAssertTrue(protocolInstance.optionalProperty ?? false)
189    verify(protocolMock.optionalProperty).wasCalled()
190  }
191  
192  func testOptionalSubscriptGetterGlobalForwarding() {
193    protocolMock.forwardCalls(to: protocolFake)
194    XCTAssertTrue(protocolInstance[1] ?? false)
195    verify(protocolMock[any()]).wasCalled()
196  }
197  func testOptionalSubscriptGetterLocalForwarding() {
198    given(protocolMock[any()]).willForward(to: protocolFake)
199    XCTAssertTrue(protocolInstance[1] ?? false)
200    verify(protocolMock[any()]).wasCalled()
201  }
202}
203
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)