How to use MinimalObjCClass class

Best Mockingbird code snippet using .MinimalObjCClass

Run Mockingbird automation tests on LambdaTest cloud grid

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

DynamicSwiftTests.swift

Source: DynamicSwiftTests.swift Github

copy
1//
2//  ObjectiveCTests.swift
3//  MockingbirdTests
4//
5//  Created by typealias on 7/17/21.
6//
7
8import Mockingbird
9@testable import MockingbirdTestsHost
10import XCTest
11
12@objc class MinimalObjCClass: Foundation.NSObject {
13  @objc dynamic func method(valueType: Bool) -> Bool { fatalError() }
14  @objc dynamic func method(bridgedType: String) -> String { fatalError() }
15  @objc dynamic func method(referenceType: Foundation.NSObject) -> Foundation.NSObject {
16    fatalError()
17  }
18  @objc dynamic func method(first: String, second: String) -> String { fatalError() }
19  
20  @objc dynamic func throwingMethod() throws {}
21  @objc dynamic public func trivialMethod() {}
22  
23  @objc dynamic var valueTypeProperty = false
24  @objc dynamic var bridgedTypeProperty = ""
25  @objc dynamic var referenceTypeProperty = Foundation.NSObject()
26}
27
28class MinimalObjCSubclass: MinimalObjCClass {}
29
30class DynamicSwiftTests: BaseTestCase {
31  
32  var classMock: MinimalObjCClass!
33  var subclassMock: MinimalObjCSubclass!
34  
35  override func setUpWithError() throws {
36    self.classMock = mock(MinimalObjCClass.self)
37    self.subclassMock = mock(MinimalObjCSubclass.self)
38  }
39  
40  
41  // MARK: - Swift Properties
42  
43  // MARK: Value types
44  
45  func testClassValueTypePropertyGetter() throws {
46    given(classMock.valueTypeProperty).willReturn(true)
47    XCTAssertTrue(classMock.valueTypeProperty)
48    verify(classMock.valueTypeProperty).wasCalled()
49  }
50  func testClassValueTypePropertyGetter_stubbingOperator() throws {
51    given(classMock.valueTypeProperty) ~> true
52    XCTAssertTrue(classMock.valueTypeProperty)
53    verify(classMock.valueTypeProperty).wasCalled()
54  }
55  
56  func testClassValueTypePropertySetter() throws {
57    let expectation = XCTestExpectation()
58    given(classMock.valueTypeProperty = true).will { (newValue: Bool) in
59      XCTAssertTrue(newValue)
60      expectation.fulfill()
61    }
62    classMock.valueTypeProperty = true
63    verify(classMock.valueTypeProperty = true).wasCalled()
64    wait(for: [expectation], timeout: 2)
65  }
66  func testClassValueTypePropertySetter_stubbingOperator() throws {
67    let expectation = XCTestExpectation()
68    given(classMock.valueTypeProperty = true) ~> { (newValue: Bool) in
69      XCTAssertTrue(newValue)
70      expectation.fulfill()
71    }
72    classMock.valueTypeProperty = true
73    verify(classMock.valueTypeProperty = true).wasCalled()
74    wait(for: [expectation], timeout: 2)
75  }
76  
77  func testClassValueTypePropertySetterMatchesWildcard() throws {
78    let expectation = XCTestExpectation()
79    given(classMock.valueTypeProperty = any()).will { (newValue: Bool) in
80      XCTAssertTrue(newValue)
81      expectation.fulfill()
82    }
83    classMock.valueTypeProperty = true
84    verify(classMock.valueTypeProperty = any()).wasCalled()
85    wait(for: [expectation], timeout: 2)
86  }
87  func testClassValueTypePropertySetterMatchesWildcard_stubbingOperator() throws {
88    let expectation = XCTestExpectation()
89    given(classMock.valueTypeProperty = any()) ~> { (newValue: Bool) in
90      XCTAssertTrue(newValue)
91      expectation.fulfill()
92    }
93    classMock.valueTypeProperty = true
94    verify(classMock.valueTypeProperty = any()).wasCalled()
95    wait(for: [expectation], timeout: 2)
96  }
97  
98  // MARK: Bridged types
99  
100  func testClassBridgedPropertyGetter() throws {
101    given(classMock.bridgedTypeProperty).willReturn("Ryan")
102    XCTAssertEqual(classMock.bridgedTypeProperty, "Ryan")
103    verify(classMock.bridgedTypeProperty).wasCalled()
104  }
105  func testClassBridgedPropertyGetter_stubbingOperator() throws {
106    given(classMock.bridgedTypeProperty) ~> "Ryan"
107    XCTAssertEqual(classMock.bridgedTypeProperty, "Ryan")
108    verify(classMock.bridgedTypeProperty).wasCalled()
109  }
110  
111  func testClassBridgedPropertySetter() throws {
112    let expectation = XCTestExpectation()
113    given(classMock.bridgedTypeProperty = "Ryan").will { (newValue: String) in
114      XCTAssertEqual(newValue, "Ryan")
115      expectation.fulfill()
116    }
117    classMock.bridgedTypeProperty = "Ryan"
118    verify(classMock.bridgedTypeProperty = "Ryan").wasCalled()
119    wait(for: [expectation], timeout: 2)
120  }
121  func testClassBridgedPropertySetter_stubbingOperator() throws {
122    let expectation = XCTestExpectation()
123    given(classMock.bridgedTypeProperty = "Ryan") ~> { (newValue: String) in
124      XCTAssertEqual(newValue, "Ryan")
125      expectation.fulfill()
126    }
127    classMock.bridgedTypeProperty = "Ryan"
128    verify(classMock.bridgedTypeProperty = "Ryan").wasCalled()
129    wait(for: [expectation], timeout: 2)
130  }
131  
132  func testClassBridgedPropertySetterMatchesWildcard() throws {
133    let expectation = XCTestExpectation()
134    given(classMock.bridgedTypeProperty = any()).will { (newValue: String) in
135      XCTAssertEqual(newValue, "Ryan")
136      expectation.fulfill()
137    }
138    classMock.bridgedTypeProperty = "Ryan"
139    verify(classMock.bridgedTypeProperty = any()).wasCalled()
140    wait(for: [expectation], timeout: 2)
141  }
142  func testClassBridgedPropertySetterMatchesWildcard_stubbingOperator() throws {
143    let expectation = XCTestExpectation()
144    given(classMock.bridgedTypeProperty = any()) ~> { (newValue: String) in
145      XCTAssertEqual(newValue, "Ryan")
146      expectation.fulfill()
147    }
148    classMock.bridgedTypeProperty = "Ryan"
149    verify(classMock.bridgedTypeProperty = any()).wasCalled()
150    wait(for: [expectation], timeout: 2)
151  }
152  
153  // MARK: Reference types
154  
155  func testClassReferenceTypePropertyGetter() throws {
156    let ref = Foundation.NSObject()
157    given(classMock.referenceTypeProperty).willReturn(ref)
158    XCTAssertTrue(classMock.referenceTypeProperty === ref)
159    verify(classMock.referenceTypeProperty).wasCalled()
160  }
161  func testClassReferenceTypePropertyGetter_stubbingOperator() throws {
162    let ref = Foundation.NSObject()
163    given(classMock.referenceTypeProperty) ~> ref
164    XCTAssertTrue(classMock.referenceTypeProperty === ref)
165    verify(classMock.referenceTypeProperty).wasCalled()
166  }
167  
168  func testClassReferenceTypePropertySetter() throws {
169    let ref = Foundation.NSObject()
170    let expectation = XCTestExpectation()
171    given(classMock.referenceTypeProperty = ref).will { (newValue: Foundation.NSObject) in
172      XCTAssertEqual(newValue, ref)
173      expectation.fulfill()
174    }
175    classMock.referenceTypeProperty = ref
176    verify(classMock.referenceTypeProperty = ref).wasCalled()
177    wait(for: [expectation], timeout: 2)
178  }
179  func testClassReferenceTypePropertySetter_stubbingOperator() throws {
180    let ref = Foundation.NSObject()
181    let expectation = XCTestExpectation()
182    given(classMock.referenceTypeProperty = ref) ~> { (newValue: Foundation.NSObject) in
183      XCTAssertEqual(newValue, ref)
184      expectation.fulfill()
185    }
186    classMock.referenceTypeProperty = ref
187    verify(classMock.referenceTypeProperty = ref).wasCalled()
188    wait(for: [expectation], timeout: 2)
189  }
190  
191  func testClassReferenceTypePropertySetterExclusive() throws {
192    given(self.classMock.referenceTypeProperty = Foundation.NSObject()).will {
193      (newValue: Foundation.NSObject) in
194      XCTFail()
195    }
196    self.classMock.referenceTypeProperty = Foundation.NSObject()
197  }
198  func testClassReferenceTypePropertySetterExclusive_stubbingOperator() throws {
199    given(self.classMock.referenceTypeProperty = Foundation.NSObject()) ~> {
200      (newValue: Foundation.NSObject) in
201      XCTFail()
202    }
203    self.classMock.referenceTypeProperty = Foundation.NSObject()
204  }
205  
206  func testClassReferenceTypePropertySetterMatchesWildcard() throws {
207    let ref = Foundation.NSObject()
208    let expectation = XCTestExpectation()
209    given(classMock.referenceTypeProperty = any()).will {
210      (newValue: Foundation.NSObject) in
211      XCTAssertEqual(newValue, ref)
212      expectation.fulfill()
213    }
214    classMock.referenceTypeProperty = ref
215    verify(classMock.referenceTypeProperty = any()).wasCalled()
216    wait(for: [expectation], timeout: 2)
217  }
218  func testClassReferenceTypePropertySetterMatchesWildcard_stubbingOperator() throws {
219    let ref = Foundation.NSObject()
220    let expectation = XCTestExpectation()
221    given(classMock.referenceTypeProperty = any()) ~> {
222      (newValue: Foundation.NSObject) in
223      XCTAssertEqual(newValue, ref)
224      expectation.fulfill()
225    }
226    classMock.referenceTypeProperty = ref
227    verify(classMock.referenceTypeProperty = any()).wasCalled()
228    wait(for: [expectation], timeout: 2)
229  }
230  
231  
232  // MARK: - Swift methods
233  
234  // MARK: Value types
235  
236  func testClassValueTypeMethodMatchesExact() throws {
237    given(classMock.method(valueType: true)).willReturn(true)
238    XCTAssertTrue(classMock.method(valueType: true))
239    verify(classMock.method(valueType: true)).wasCalled()
240    verify(classMock.method(bridgedType: any())).wasNeverCalled()
241    verify(classMock.method(referenceType: any())).wasNeverCalled()
242  }
243  func testClassValueTypeMethodMatchesExact_stubbingOperator() throws {
244    given(classMock.method(valueType: true)) ~> true
245    XCTAssertTrue(classMock.method(valueType: true))
246    verify(classMock.method(valueType: true)).wasCalled()
247    verify(classMock.method(bridgedType: any())).wasNeverCalled()
248    verify(classMock.method(referenceType: any())).wasNeverCalled()
249  }
250  
251  func testClassValueTypeMethodMatchesExactExclusive() throws {
252    shouldFail {
253      given(self.classMock.method(valueType: true)).willReturn(true)
254      _ = self.classMock.method(valueType: false)
255    }
256  }
257  func testClassValueTypeMethodMatchesExactExclusive_stubbingOperator() throws {
258    shouldFail {
259      given(self.classMock.method(valueType: true)) ~> true
260      _ = self.classMock.method(valueType: false)
261    }
262  }
263  
264  func testClassValueTypeMethodMatchesWildcard() throws {
265    given(classMock.method(valueType: firstArg(any()))).will {
266      (valueType: Bool) in
267      return valueType
268    }
269    XCTAssertTrue(classMock.method(valueType: true))
270    XCTAssertFalse(classMock.method(valueType: false))
271    verify(classMock.method(valueType: true)).wasCalled()
272    verify(classMock.method(valueType: false)).wasCalled()
273    verify(classMock.method(bridgedType: any())).wasNeverCalled()
274    verify(classMock.method(referenceType: any())).wasNeverCalled()
275  }
276  func testClassValueTypeMethodMatchesWildcard_stubbingOperator() throws {
277    given(classMock.method(valueType: any())) ~> {
278      (valueType: Bool) in
279      return valueType
280    }
281    XCTAssertTrue(classMock.method(valueType: true))
282    XCTAssertFalse(classMock.method(valueType: false))
283    verify(classMock.method(valueType: true)).wasCalled()
284    verify(classMock.method(valueType: false)).wasCalled()
285    verify(classMock.method(bridgedType: any())).wasNeverCalled()
286    verify(classMock.method(referenceType: any())).wasNeverCalled()
287  }
288  
289  // MARK: Bridged types
290  
291  func testClassBridgedTypeMethodMatchesExact() throws {
292    given(classMock.method(bridgedType: "Ryan")).willReturn("Ryan")
293    XCTAssertEqual(classMock.method(bridgedType: "Ryan"), "Ryan")
294    verify(classMock.method(valueType: any())).wasNeverCalled()
295    verify(classMock.method(bridgedType: "Ryan")).wasCalled()
296    verify(classMock.method(referenceType: any())).wasNeverCalled()
297  }
298  func testClassBridgedTypeMethodMatchesExact_stubbingOperator() throws {
299    given(classMock.method(bridgedType: "Ryan")) ~> "Ryan"
300    XCTAssertEqual(classMock.method(bridgedType: "Ryan"), "Ryan")
301    verify(classMock.method(valueType: any())).wasNeverCalled()
302    verify(classMock.method(bridgedType: "Ryan")).wasCalled()
303    verify(classMock.method(referenceType: any())).wasNeverCalled()
304  }
305  
306  func testClassBridgedTypeMethodMatchesExactExclusive() throws {
307    shouldFail {
308      given(self.classMock.method(bridgedType: "Ryan")).willReturn("Ryan")
309      _ = self.classMock.method(bridgedType: "Sterling")
310    }
311  }
312  func testClassBridgedTypeMethodMatchesExactExclusive_stubbingOperator() throws {
313    shouldFail {
314      given(self.classMock.method(bridgedType: "Ryan")) ~> "Ryan"
315      _ = self.classMock.method(bridgedType: "Sterling")
316    }
317  }
318  
319  func testClassBridgedTypeMethodMatchesWildcard() throws {
320    given(classMock.method(bridgedType: any())).will {
321      (valueType: String) in
322      return valueType
323    }
324    XCTAssertEqual(classMock.method(bridgedType: "Ryan"), "Ryan")
325    XCTAssertEqual(classMock.method(bridgedType: "Sterling"), "Sterling")
326    verify(classMock.method(valueType: any())).wasNeverCalled()
327    verify(classMock.method(bridgedType: "Ryan")).wasCalled()
328    verify(classMock.method(bridgedType: "Sterling")).wasCalled()
329    verify(classMock.method(referenceType: any())).wasNeverCalled()
330  }
331  func testClassBridgedTypeMethodMatchesWildcard_stubbingOperator() throws {
332    given(classMock.method(bridgedType: any())) ~> {
333      (valueType: String) in
334      return valueType
335    }
336    XCTAssertEqual(classMock.method(bridgedType: "Ryan"), "Ryan")
337    XCTAssertEqual(classMock.method(bridgedType: "Sterling"), "Sterling")
338    verify(classMock.method(valueType: any())).wasNeverCalled()
339    verify(classMock.method(bridgedType: "Ryan")).wasCalled()
340    verify(classMock.method(bridgedType: "Sterling")).wasCalled()
341    verify(classMock.method(referenceType: any())).wasNeverCalled()
342  }
343  
344  // MARK: Reference types
345  
346  func testClassReferenceTypeMethodMatchesExact() throws {
347    let ref = Foundation.NSObject()
348    given(classMock.method(referenceType: ref)).willReturn(ref)
349    XCTAssertEqual(classMock.method(referenceType: ref), ref)
350    verify(classMock.method(valueType: any())).wasNeverCalled()
351    verify(classMock.method(bridgedType: any())).wasNeverCalled()
352    verify(classMock.method(referenceType: ref)).wasCalled()
353  }
354  func testClassReferenceTypeMethodMatchesExact_stubbingOperator() throws {
355    let ref = Foundation.NSObject()
356    given(classMock.method(referenceType: ref)) ~> ref
357    XCTAssertEqual(classMock.method(referenceType: ref), ref)
358    verify(classMock.method(valueType: any())).wasNeverCalled()
359    verify(classMock.method(bridgedType: any())).wasNeverCalled()
360    verify(classMock.method(referenceType: ref)).wasCalled()
361  }
362  
363  func testClassReferenceTypeMethodMatchesExactExclusive() throws {
364    shouldFail {
365      let ref = Foundation.NSObject()
366      given(self.classMock.method(referenceType: ref)).willReturn(ref)
367      _ = self.classMock.method(referenceType: Foundation.NSObject())
368    }
369  }
370  func testClassReferenceTypeMethodMatchesExactExclusive_stubbingOperator() throws {
371    shouldFail {
372      let ref = Foundation.NSObject()
373      given(self.classMock.method(referenceType: ref)) ~> ref
374      _ = self.classMock.method(referenceType: Foundation.NSObject())
375    }
376  }
377  
378  func testClassReferenceTypeMethodMatchesWildcard() throws {
379    given(classMock.method(referenceType: any())).will {
380      (valueType: Foundation.NSObject) in
381      return valueType
382    }
383    let ref1 = Foundation.NSObject()
384    let ref2 = Foundation.NSObject()
385    XCTAssertEqual(classMock.method(referenceType: ref1), ref1)
386    XCTAssertEqual(classMock.method(referenceType: ref2), ref2)
387    verify(classMock.method(valueType: any())).wasNeverCalled()
388    verify(classMock.method(bridgedType: any())).wasNeverCalled()
389    verify(classMock.method(referenceType: any())).wasCalled(twice)
390  }
391  func testClassReferenceTypeMethodMatchesWildcard_stubbingOperator() throws {
392    given(classMock.method(referenceType: any())) ~> {
393      (valueType: Foundation.NSObject) in
394      return valueType
395    }
396    let ref1 = Foundation.NSObject()
397    let ref2 = Foundation.NSObject()
398    XCTAssertEqual(classMock.method(referenceType: ref1), ref1)
399    XCTAssertEqual(classMock.method(referenceType: ref2), ref2)
400    verify(classMock.method(valueType: any())).wasNeverCalled()
401    verify(classMock.method(bridgedType: any())).wasNeverCalled()
402    verify(classMock.method(referenceType: any())).wasCalled(twice)
403  }
404  
405  // MARK: Multiple parameters
406  
407  func testClassMultipleParameterMethod_matchesHomogenousWildcard() throws {
408    given(classMock.method(first: any(), second: any())).will {
409      (first: String, second: String) in
410      return first + "-" + second
411    }
412    XCTAssertEqual(classMock.method(first: "a", second: "b"), "a-b")
413    verify(classMock.method(first: any(), second: any())).wasCalled()
414  }
415  
416  func testClassMultipleParameterMethod_matchesHomogenousWildcard_explicitIndexes() throws {
417    given(classMock.method(first: firstArg(any()), second: secondArg(any()))).will {
418      (first: String, second: String) in
419      return first + "-" + second
420    }
421    XCTAssertEqual(classMock.method(first: "a", second: "b"), "a-b")
422    verify(classMock.method(first: firstArg(any()), second: secondArg(any()))).wasCalled()
423  }
424  
425  func testClassMultipleParameterMethod_matchesHeterogenousWildcardFirst_explicitIndexes() throws {
426    given(classMock.method(first: "a", second: secondArg(any()))).will {
427      (first: String, second: String) in
428      return first + "-" + second
429    }
430    XCTAssertEqual(classMock.method(first: "a", second: "b"), "a-b")
431    verify(classMock.method(first: "a", second: secondArg(any()))).wasCalled()
432  }
433  
434  func testClassMultipleParameterMethod_matchesHeterogenousWildcardSecond_explicitIndexes() throws {
435    given(classMock.method(first: firstArg(any()), second: "b")).will {
436      (first: String, second: String) in
437      return first + "-" + second
438    }
439    XCTAssertEqual(classMock.method(first: "a", second: "b"), "a-b")
440    verify(classMock.method(first: firstArg(any()), second: "b")).wasCalled()
441  }
442  
443  func testClassMultipleParameterMethod_failsStubbingHeterogenous() throws {
444    shouldFail {
445      given(self.classMock.method(first: "a", second: any())).willReturn("foo")
446    }
447    shouldFail {
448      given(self.classMock.method(first: any(), second: "b")).willReturn("foo")
449    }
450  }
451  
452  func testClassMultipleParameterMethod_failsVerificationHeterogenous() throws {
453    shouldFail {
454      verify(self.classMock.method(first: "a", second: any())).wasNeverCalled()
455    }
456    shouldFail {
457      verify(self.classMock.method(first: any(), second: "b")).wasNeverCalled()
458    }
459  }
460  
461  
462  // MARK: - Throwing
463  
464  func testThrowNSError() throws {
465    given(try classMock.throwingMethod())
466      .willThrow(NSError(domain: "co.bird.mockingbird.error", code: 1, userInfo: nil))
467    XCTAssertThrowsError(try classMock.throwingMethod(), "Mock should throw", { error in
468      XCTAssertEqual((error as NSError).domain, "co.bird.mockingbird.error")
469      XCTAssertEqual((error as NSError).code, 1)
470    })
471    verify(try classMock.throwingMethod()).wasCalled()
472  }
473  
474  func testThrowSwiftErrorStruct() throws {
475    struct FakeError: LocalizedError {
476      let errorDescription: String? = "foobar"
477    }
478    given(try classMock.throwingMethod()).willThrow(FakeError())
479    XCTAssertThrowsError(try classMock.throwingMethod(), "Mock should throw", { error in
480      XCTAssertEqual(error.localizedDescription, "foobar")
481    })
482    verify(try classMock.throwingMethod()).wasCalled()
483  }
484  
485  func testThrowSwiftErrorClass() throws {
486    class FakeError: LocalizedError {
487      let errorDescription: String? = "foobar"
488    }
489    given(try classMock.throwingMethod()).willThrow(FakeError())
490    XCTAssertThrowsError(try classMock.throwingMethod(), "Mock should throw", { error in
491      XCTAssertEqual(error.localizedDescription, "foobar")
492    })
493    verify(try classMock.throwingMethod()).wasCalled()
494  }
495  
496  func testThrowFromClosure() throws {
497    struct FakeError: LocalizedError {
498      let errorDescription: String? = "foobar"
499    }
500    given(try classMock.throwingMethod()).will { throw FakeError() }
501    XCTAssertThrowsError(try classMock.throwingMethod(), "Mock should throw", { error in
502      XCTAssertEqual(error.localizedDescription, "foobar")
503    })
504    verify(try classMock.throwingMethod()).wasCalled()
505  }
506  
507  func testThrowingOnNonThrowingMethod() throws {
508    struct FakeError: Error {}
509    given(classMock.trivialMethod()).willThrow(FakeError())
510    classMock.trivialMethod()
511    verify(classMock.trivialMethod()).wasCalled()
512  }
513  
514  func testSubclass() throws {
515    let expectation = XCTestExpectation()
516    expectation.expectedFulfillmentCount = 2
517    given(subclassMock.method(valueType: any())).will { (val: Bool) in
518      expectation.fulfill()
519      return val
520    }
521    XCTAssertTrue(subclassMock.method(valueType: true))
522    XCTAssertFalse(subclassMock.method(valueType: false))
523    verify(subclassMock.method(valueType: true)).wasCalled()
524    verify(subclassMock.method(valueType: false)).wasCalled()
525    verify(subclassMock.method(valueType: any())).wasCalled(twice)
526    wait(for: [expectation], timeout: 2)
527  }
528}
529
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

Most used methods in MinimalObjCClass

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)