How to use testStubParameterizedMethod_onProtocolMock_withImplicitlyTypedClosure method of StubbingTests class

Best Mockingbird code snippet using StubbingTests.testStubParameterizedMethod_onProtocolMock_withImplicitlyTypedClosure

Run Mockingbird automation tests on LambdaTest cloud grid

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

StubbingTests.swift

Source: StubbingTests.swift Github

copy
1//
2//  StubbingTests.swift
3//  MockingbirdTests
4//
5//  Created by Andrew Chang on 8/18/19.
6//  Copyright © 2019 Bird Rides, Inc. All rights reserved.
7//
8
9import Mockingbird
10import XCTest
11@testable import MockingbirdTestsHost
12
13class StubbingTests: BaseTestCase {
14  
15  struct FakeError: Error {}
16  
17  var child: ChildMock!
18  var childInstance: Child { return child }
19  
20  var childProtocol: ChildProtocolMock!
21  var childProtocolInstance: ChildProtocol { return childProtocol }
22  
23  override func setUp() {
24    child = mock(Child.self)
25    childProtocol = mock(ChildProtocol.self)
26  }
27  
28  func testStubTrivialMethod_onClassMock_implicitlyStubbed() {
29    childInstance.childTrivialInstanceMethod()
30    verify(child.childTrivialInstanceMethod()).wasCalled()
31  }
32  func testStubTrivialMethod_onProtocolMock_implicitlyStubbed() {
33    childProtocolInstance.childTrivialInstanceMethod()
34    verify(childProtocol.childTrivialInstanceMethod()).wasCalled()
35  }
36  
37  func testStubTrivialMethod_onClassMock() {
38    given(child.childTrivialInstanceMethod()) ~> ()
39    childInstance.childTrivialInstanceMethod()
40    verify(child.childTrivialInstanceMethod()).wasCalled()
41  }
42  func testStubTrivialMethod_onProtocolMock() {
43    given(childProtocol.childTrivialInstanceMethod()) ~> ()
44    childProtocolInstance.childTrivialInstanceMethod()
45    verify(childProtocol.childTrivialInstanceMethod()).wasCalled()
46  }
47  
48  func testStubTrivialMethod_onClassMock_explicitSyntax() {
49    given(child.childTrivialInstanceMethod()).willReturn(())
50    childInstance.childTrivialInstanceMethod()
51    verify(child.childTrivialInstanceMethod()).wasCalled()
52  }
53  func testStubTrivialMethod_onProtocolMock_explicitSyntax() {
54    given(childProtocol.childTrivialInstanceMethod()).willReturn(())
55    childProtocolInstance.childTrivialInstanceMethod()
56    verify(childProtocol.childTrivialInstanceMethod()).wasCalled()
57  }
58  
59  func testStubTrivialMethod_onClassMock_convenienceExplicitSyntax() {
60    given(child.childTrivialInstanceMethod()).willReturn()
61    childInstance.childTrivialInstanceMethod()
62    verify(child.childTrivialInstanceMethod()).wasCalled()
63  }
64  func testStubTrivialMethod_onProtocolMock_convenienceExplicitSyntax() {
65    given(childProtocol.childTrivialInstanceMethod()).willReturn()
66    childProtocolInstance.childTrivialInstanceMethod()
67    verify(childProtocol.childTrivialInstanceMethod()).wasCalled()
68  }
69  
70  func testStubParameterizedMethodWithWildcardMatcher_onClassMock() {
71    given(child.childParameterizedInstanceMethod(param1: any(), any())) ~> true
72    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
73    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
74    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
75  }
76  func testStubParameterizedMethodWithWildcardMatcher_onProtocolMock() {
77    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())) ~> true
78    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
79    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
80    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
81  }
82  
83  func testStubParameterizedMethodWithWildcardMatcher_onClassMock_explicitSyntax() {
84    given(child.childParameterizedInstanceMethod(param1: any(), any())).willReturn(true)
85    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
86    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
87    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
88  }
89  func testStubParameterizedMethodWithWildcardMatcher_onProtocolMock_explicitSyntax() {
90    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).willReturn(true)
91    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
92    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
93    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
94  }
95  
96  func testStubParameterizedMethodWithExactValue_onClassMock() {
97    given(child.childParameterizedInstanceMethod(param1: true, 1)) ~> true
98    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
99    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
100  }
101  func testStubParameterizedMethodWithExactValue_onProtocolMock() {
102    given(childProtocol.childParameterizedInstanceMethod(param1: true, 1)) ~> true
103    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
104    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
105  }
106  
107  func testStubParameterizedMethodWithExactValue_onClassMock_explicitSyntax() {
108    given(child.childParameterizedInstanceMethod(param1: true, 1)).willReturn(true)
109    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
110    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
111  }
112  func testStubParameterizedMethodWithExactValue_onProtocolMock_explicitSyntax() {
113    given(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).willReturn(true)
114    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
115    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
116  }
117  
118  // MARK: Non-matching
119  
120  func testStubParameterizedMethodWithWildcardMatcher_doesNotMatch_onClassMock() {
121    shouldFail {
122      given(self.child.childParameterizedInstanceMethod(param1: any(), 1)) ~> true
123      XCTAssertTrue(self.childInstance.childParameterizedInstanceMethod(param1: true, 2))
124    }
125  }
126  func testStubParameterizedMethodWithWildcardMatcher_doesNotMatch_onProtocolMock() {
127    shouldFail {
128      given(self.childProtocol.childParameterizedInstanceMethod(param1: any(), 1)) ~> true
129      XCTAssertTrue(self.childProtocolInstance.childParameterizedInstanceMethod(param1: true, 2))
130    }
131  }
132  
133  func testStubParameterizedMethodWithWildcardMatcher_doesNotMatch_onClassMock_explicitSyntax() {
134    shouldFail {
135      given(self.child.childParameterizedInstanceMethod(param1: any(), 1)).willReturn(true)
136      XCTAssertTrue(self.childInstance.childParameterizedInstanceMethod(param1: true, 2))
137    }
138  }
139  func testStubParameterizedMethodWithWildcardMatcher_doesNotMatch_onProtocolMock_explicitSyntax() {
140    shouldFail {
141      given(self.childProtocol.childParameterizedInstanceMethod(param1: any(), 1)).willReturn(true)
142      XCTAssertTrue(self.childProtocolInstance.childParameterizedInstanceMethod(param1: true, 2))
143    }
144  }
145  
146  func testStubParameterizedMethodWithExactValue_doesNotMatch_onClassMock() {
147    shouldFail {
148      given(self.child.childParameterizedInstanceMethod(param1: true, 1)) ~> true
149      XCTAssertTrue(self.childInstance.childParameterizedInstanceMethod(param1: false, 1))
150    }
151  }
152  func testStubParameterizedMethodWithExactValue_doesNotMatch_onProtocolMock() {
153    shouldFail {
154      given(self.childProtocol.childParameterizedInstanceMethod(param1: true, 1)) ~> true
155      XCTAssertTrue(self.childProtocolInstance.childParameterizedInstanceMethod(param1: false, 1))
156    }
157  }
158  
159  func testStubParameterizedMethodWithExactValue_doesNotMatch_onClassMock_explicitSyntax() {
160    shouldFail {
161      given(self.child.childParameterizedInstanceMethod(param1: true, 1)).willReturn(true)
162      XCTAssertTrue(self.childInstance.childParameterizedInstanceMethod(param1: false, 1))
163    }
164  }
165  func testStubParameterizedMethodWithExactValue_doesNotMatch_onProtocolMock_explicitSyntax() {
166    shouldFail {
167      given(self.childProtocol.childParameterizedInstanceMethod(param1: true, 1)).willReturn(true)
168      XCTAssertTrue(self.childProtocolInstance.childParameterizedInstanceMethod(param1: false, 1))
169    }
170  }
171  
172  // MARK: Value consistency
173  
174  func testStubReturnValueReturnsSameValue_onClassMock() {
175    given(child.getChildComputedInstanceVariable()) ~> true
176    XCTAssertTrue(childInstance.childComputedInstanceVariable)
177    XCTAssertTrue(childInstance.childComputedInstanceVariable)
178    XCTAssertTrue(childInstance.childComputedInstanceVariable)
179    verify(child.getChildComputedInstanceVariable()).wasCalled(exactly(3))
180  }
181  func testStubReturnValueReturnsSameValue_onProtocolMock() {
182    given(childProtocol.getChildInstanceVariable()) ~> true
183    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
184    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
185    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
186    verify(childProtocol.getChildInstanceVariable()).wasCalled(exactly(3))
187  }
188  
189  func testStubReturnValueReturnsSameValue_onClassMock_explicitSyntax() {
190    given(child.getChildComputedInstanceVariable()).willReturn(true)
191    XCTAssertTrue(childInstance.childComputedInstanceVariable)
192    XCTAssertTrue(childInstance.childComputedInstanceVariable)
193    XCTAssertTrue(childInstance.childComputedInstanceVariable)
194    verify(child.getChildComputedInstanceVariable()).wasCalled(exactly(3))
195  }
196  func testStubReturnValueReturnsSameValue_onProtocolMock_explicitSyntax() {
197    given(childProtocol.getChildInstanceVariable()).willReturn(true)
198    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
199    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
200    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
201    verify(childProtocol.getChildInstanceVariable()).wasCalled(exactly(3))
202  }
203  
204  // MARK: Precedence
205  
206  func testStubParameterizedMethodOverridesPrevious_onClassMock() {
207    given(child.childParameterizedInstanceMethod(param1: any(), any())) ~> true
208    given(child.childParameterizedInstanceMethod(param1: any(), any())) ~> false
209    XCTAssertFalse(childInstance.childParameterizedInstanceMethod(param1: true, 1))
210    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
211    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
212  }
213  func testStubParameterizedMethodOverridesPrevious_onProtocolMock() {
214    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())) ~> true
215    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())) ~> false
216    XCTAssertFalse(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
217    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
218    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
219  }
220  
221  func testStubParameterizedMethodOverridesPrevious_onClassMock_explicitSyntax() {
222    given(child.childParameterizedInstanceMethod(param1: any(), any())).willReturn(true)
223    given(child.childParameterizedInstanceMethod(param1: any(), any())).willReturn(false)
224    XCTAssertFalse(childInstance.childParameterizedInstanceMethod(param1: true, 1))
225    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
226    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
227  }
228  func testStubParameterizedMethodOverridesPrevious_onProtocolMock_explicitSyntax() {
229    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).willReturn(true)
230    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).willReturn(false)
231    XCTAssertFalse(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
232    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
233    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
234  }
235  
236  func testStubParameterizedMethodIgnoresNonMatching_onClassMock() {
237    given(child.childParameterizedInstanceMethod(param1: any(), any())) ~> true
238    given(child.childParameterizedInstanceMethod(param1: any(), 100)) ~> false
239    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
240    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
241    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
242  }
243  func testStubParameterizedMethodIgnoresNonMatching_onProtocolMock() {
244    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())) ~> true
245    given(childProtocol.childParameterizedInstanceMethod(param1: any(), 100)) ~> false
246    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
247    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
248    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
249  }
250  
251  func testStubParameterizedMethodIgnoresNonMatching_onClassMock_explicitSyntax() {
252    given(child.childParameterizedInstanceMethod(param1: any(), any())).willReturn(true)
253    given(child.childParameterizedInstanceMethod(param1: any(), 100)).willReturn(false)
254    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
255    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
256    verify(child.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
257  }
258  func testStubParameterizedMethodIgnoresNonMatching_onProtocolMock_explicitSyntax() {
259    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).willReturn(true)
260    given(childProtocol.childParameterizedInstanceMethod(param1: any(), 100)).willReturn(false)
261    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
262    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
263    verify(childProtocol.childParameterizedInstanceMethod(param1: true, 1)).wasCalled()
264  }
265  
266  // MARK: Clearing stubs
267  
268  func testClearStubs_onClassMock() {
269    shouldFail {
270      given(self.child.getChildStoredInstanceVariable()) ~> true
271      clearStubs(on: self.child)
272      XCTAssertTrue(self.child.childStoredInstanceVariable)
273    }
274  }
275  func testClearStubs_onProtocolMock() {
276    shouldFail {
277      given(self.childProtocol.getChildInstanceVariable()) ~> true
278      clearStubs(on: self.childProtocol)
279      XCTAssertTrue(self.child.childComputedInstanceVariable)
280    }
281  }
282  
283  func testClearStubs_onClassMock_explicitSyntax() {
284    shouldFail {
285      given(self.child.getChildStoredInstanceVariable()).willReturn(true)
286      clearStubs(on: self.child)
287      XCTAssertTrue(self.child.childStoredInstanceVariable)
288    }
289  }
290  func testClearStubs_onProtocolMock_explicitSyntax() {
291    shouldFail {
292      given(self.childProtocol.getChildInstanceVariable()).willReturn(true)
293      clearStubs(on: self.childProtocol)
294      XCTAssertTrue(self.child.childComputedInstanceVariable)
295    }
296  }
297  
298  // MARK: Closure implementation stubs
299  
300  func testStubParameterizedMethod_onClassMock_withExplicitlyTypedClosure() {
301    given(child.childParameterizedInstanceMethod(param1: any(), any())) ~> {
302      (param1: Bool, param2: Int) -> Bool in
303      return param1 && param2 == 1
304    }
305    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
306    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
307  }
308  func testStubParameterizedMethod_onProtocolMock_withExplicitlyTypedClosure() {
309    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any())) ~> {
310      (param1: Bool, param2: Int) -> Bool in
311      return param1 && param2 == 1
312    }
313    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
314    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
315  }
316  
317  func testStubParameterizedMethod_onClassMock_withImplicitlyTypedClosure() {
318    given(child.childParameterizedInstanceMethod(param1: any(), any()))
319      ~> { $0 && $1 == 1 }
320    XCTAssertTrue(childInstance.childParameterizedInstanceMethod(param1: true, 1))
321    verify(child.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
322  }
323  func testStubParameterizedMethod_onProtocolMock_withImplicitlyTypedClosure() {
324    given(childProtocol.childParameterizedInstanceMethod(param1: any(), any()))
325      ~> { $0 && $1 == 1 }
326    XCTAssertTrue(childProtocolInstance.childParameterizedInstanceMethod(param1: true, 1))
327    verify(childProtocol.childParameterizedInstanceMethod(param1: any(), any())).wasCalled()
328  }
329  
330  func testStubTrivialMethod_onClassMock_withExplicitClosure() {
331    given(child.childTrivialInstanceMethod()) ~> {}
332    childInstance.childTrivialInstanceMethod()
333    verify(child.childTrivialInstanceMethod()).wasCalled()
334  }
335  func testStubTrivialMethod_onProtocolMock_withExplicitClosure() {
336    given(childProtocol.childTrivialInstanceMethod()) ~> {}
337    childProtocolInstance.childTrivialInstanceMethod()
338    verify(childProtocol.childTrivialInstanceMethod()).wasCalled()
339  }
340  
341  func testStubNonParameterizedReturningMethod_onClassMock_withExplicitClosure() {
342    given(child.getChildComputedInstanceVariable()) ~> {true}
343    XCTAssertTrue(childInstance.childComputedInstanceVariable)
344    verify(child.getChildComputedInstanceVariable()).wasCalled()
345  }
346  func testStubNonParameterizedReturningMethod_onProtocolMock_withExplicitClosure() {
347    given(childProtocol.getChildInstanceVariable()) ~> {true}
348    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
349    verify(childProtocol.getChildInstanceVariable()).wasCalled()
350  }
351  
352  // MARK: Chained stubbing
353  
354  func testTransitionsToNextStub_afterCount() {
355    given(childProtocol.getChildInstanceVariable())
356      .willReturn(loopingSequence(of: true, false), transition: .after(4))
357      .willReturn(true)
358    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
359    XCTAssertFalse(childProtocolInstance.childInstanceVariable)
360    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
361    XCTAssertFalse(childProtocolInstance.childInstanceVariable)
362    
363    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
364    
365    // Check clamped to last value
366    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
367    
368    verify(childProtocol.getChildInstanceVariable()).wasCalled(exactly(6))
369  }
370  
371  func testTransitionsToNextStub_afterCount_skipsValueWhenStubbedLater() {
372    let stubbingManager = given(childProtocol.getChildInstanceVariable())
373      .willReturn(loopingSequence(of: false), transition: .after(2))
374    XCTAssertFalse(childProtocolInstance.childInstanceVariable)
375    XCTAssertFalse(childProtocolInstance.childInstanceVariable)
376    XCTAssertFalse(childProtocolInstance.childInstanceVariable) // Still using sequence value
377    
378    stubbingManager.willReturn(true)
379    XCTAssertTrue(childProtocolInstance.childInstanceVariable) // Should skip to latest stub
380    
381    // Check clamped to last value
382    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
383    
384    verify(childProtocol.getChildInstanceVariable()).wasCalled(exactly(5))
385  }
386  
387  func testTransitionsToNextStub_onFirstNil() {
388    given(childProtocol.getChildInstanceVariable())
389      .willReturn(finiteSequence(of: true, false))
390      .willReturn(true)
391    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
392    XCTAssertFalse(childProtocolInstance.childInstanceVariable)
393    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
394    
395    // Check clamped to last value
396    XCTAssertTrue(childProtocolInstance.childInstanceVariable)
397    
398    verify(childProtocol.getChildInstanceVariable()).wasCalled(exactly(4))
399  }
400}
401
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 method in StubbingTests

Trigger testStubParameterizedMethod_onProtocolMock_withImplicitlyTypedClosure code on LambdaTest Cloud Grid

Execute automation tests with testStubParameterizedMethod_onProtocolMock_withImplicitlyTypedClosure 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)