How to use testSubscriptClass_handlesBasicSingleParameterCalls method of MyObject class

Best Mockingbird code snippet using MyObject.testSubscriptClass_handlesBasicSingleParameterCalls

Run Mockingbird automation tests on LambdaTest cloud grid

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

SubscriptTests.swift

Source: SubscriptTests.swift Github

copy
1import XCTest
2import Mockingbird
3@testable import MockingbirdTestsHost
4
5class SubscriptTests: BaseTestCase {
6  
7  var protocolMock: SubscriptedProtocolMock!
8  var protocolInstance: SubscriptedProtocol { return protocolMock }
9  
10  var classMock: SubscriptedClassMock!
11  var classInstance: SubscriptedClass { return classMock }
12  
13  override func setUp() {
14    protocolMock = mock(SubscriptedProtocol.self)
15    classMock = mock(SubscriptedClass.self)
16  }
17  
18  private class MyObject {}
19  
20  // MARK: - Protocol mock
21  
22  // MARK: Getter
23  
24  func testSubscriptProtocol_handlesBasicSingleParameterGetter() {
25    let object = MyObject()
26    given(protocolMock.getSubscript(42)) ~> "bar"
27    given(protocolMock.getSubscript(42)) ~> true
28    given(protocolMock.getSubscript("foo")) ~> "bar"
29    given(protocolMock.getSubscript(object)) ~> 99
30    
31    XCTAssertEqual(protocolInstance[42], "bar")
32    XCTAssertEqual(protocolInstance[42], true)
33    XCTAssertEqual(protocolInstance["foo"], "bar")
34    XCTAssertEqual(protocolInstance[object], 99)
35    
36    verify(protocolMock.getSubscript(42)).returning(String.self).wasCalled()
37    verify(protocolMock.getSubscript(42)).returning(Bool.self).wasCalled()
38    verify(protocolMock.getSubscript("foo")).returning(String.self).wasCalled()
39    verify(protocolMock.getSubscript(object)).returning(Int.self).wasCalled()
40  }
41  
42  func testSubscriptProtocol_handlesMultipleParameterGetter() {
43    given(protocolMock.getSubscript(42, 99)) ~> "bar"
44    given(protocolMock.getSubscript("foo", "bar")) ~> "hello"
45    
46    XCTAssertEqual(protocolInstance[42, 99], "bar")
47    XCTAssertEqual(protocolInstance["foo", "bar"], "hello")
48    
49    verify(protocolMock.getSubscript(42, 99)).returning(String.self).wasCalled()
50    verify(protocolMock.getSubscript("foo", "bar")).returning(String.self).wasCalled()
51  }
52  
53  func testSubscriptProtocol_handlesGenericGetter() {
54    enum IndexType { case foo, bar }
55    given(protocolMock.getSubscript(IndexType.foo)) ~> IndexType.bar
56    XCTAssertEqual(protocolInstance[IndexType.foo], IndexType.bar)
57    verify(protocolMock.getSubscript(IndexType.foo)).returning(IndexType.self).wasCalled()
58  }
59  
60  // MARK: Setter
61  
62  func testSubscriptProtocol_handlesBasicSingleParameterSetter() {
63    var protocolInstance: SubscriptedProtocol = protocolMock // Allow subscript mutations
64    var callCount = 0
65    
66    given(protocolMock.setSubscript(42, newValue: "bar")) ~> { _, _ in callCount += 1 }
67    given(protocolMock.setSubscript(42, newValue: true)) ~> { _, _ in callCount += 1 }
68    given(protocolMock.setSubscript("foo", newValue: "bar")) ~> { _, _ in callCount += 1 }
69    
70    protocolInstance[42] = "bar"
71    protocolInstance[42] = true
72    protocolInstance["foo"] = "bar"
73    
74    verify(protocolMock.setSubscript(42, newValue: "bar")).wasCalled()
75    verify(protocolMock.setSubscript(42, newValue: true)).wasCalled()
76    verify(protocolMock.setSubscript("foo", newValue: "bar")).wasCalled()
77    
78    XCTAssertEqual(callCount, 3)
79  }
80  
81  func testSubscriptProtocol_handlesMultipleParameterSetter() {
82    var protocolInstance: SubscriptedProtocol = protocolMock // Allow subscript mutations
83    var callCount = 0
84    
85    given(protocolMock.setSubscript(42, 99, newValue: "bar")) ~> { _, _, _ in callCount += 1 }
86    given(protocolMock.setSubscript("foo", "bar", newValue: "hello")) ~> { _, _ in callCount += 1 }
87    
88    protocolInstance[42, 99] = "bar"
89    protocolInstance["foo", "bar"] = "hello"
90    
91    verify(protocolMock.setSubscript(42, 99, newValue: "bar")).wasCalled()
92    verify(protocolMock.setSubscript("foo", "bar", newValue: "hello")).wasCalled()
93    
94    XCTAssertEqual(callCount, 2)
95  }
96  
97  func testSubscriptProtocol_handlesGenericSetter() {
98    enum IndexType { case foo, bar }
99    var protocolInstance: SubscriptedProtocol = protocolMock // Allow subscript mutations
100    var callCount = 0
101    
102    given(protocolMock.setSubscript(IndexType.foo, newValue: IndexType.bar)) ~> { _, _ in
103      callCount += 1
104    }
105    protocolInstance[IndexType.foo] = IndexType.bar
106    verify(protocolMock.setSubscript(IndexType.foo, newValue: IndexType.bar)).wasCalled()
107    
108    XCTAssertEqual(callCount, 1)
109  }
110  
111  
112  // MARK: - Class mock
113  
114  func testSubscriptClass_handlesBasicSingleParameterCalls() {
115    let object = MyObject()
116    given(classMock.getSubscript(42)) ~> "bar"
117    given(classMock.getSubscript(42)) ~> true
118    given(classMock.getSubscript("foo")) ~> "bar"
119    given(classMock.getSubscript(object)) ~> 99
120    
121    XCTAssertEqual(classInstance[42], "bar")
122    XCTAssertEqual(classInstance[42], true)
123    XCTAssertEqual(classInstance["foo"], "bar")
124    XCTAssertEqual(classInstance[object], 99)
125    
126    verify(classMock.getSubscript(42)).returning(String.self).wasCalled()
127    verify(classMock.getSubscript(42)).returning(Bool.self).wasCalled()
128    verify(classMock.getSubscript("foo")).returning(String.self).wasCalled()
129    verify(classMock.getSubscript(object)).returning(Int.self).wasCalled()
130  }
131  
132  func testSubscriptClass_handlesMultipleParameterCalls() {
133    given(classMock.getSubscript(42, 99)) ~> "bar"
134    given(classMock.getSubscript("foo", "bar")) ~> "hello"
135    
136    XCTAssertEqual(classInstance[42, 99], "bar")
137    XCTAssertEqual(classInstance["foo", "bar"], "hello")
138    
139    verify(classMock.getSubscript(42, 99)).returning(String.self).wasCalled()
140    verify(classMock.getSubscript("foo", "bar")).returning(String.self).wasCalled()
141  }
142  
143  func testSubscriptClass_handlesGenericCalls() {
144    enum IndexType { case foo, bar }
145    given(classMock.getSubscript(IndexType.foo)) ~> IndexType.bar
146    XCTAssertEqual(classInstance[IndexType.foo], IndexType.bar)
147    verify(classMock.getSubscript(IndexType.foo)).returning(IndexType.self).wasCalled()
148  }
149  
150  // MARK: Setter
151  
152  func testSubscriptClass_handlesBasicSingleParameterSetter() {
153    var callCount = 0
154    
155    given(classMock.setSubscript(42, newValue: "bar")) ~> { _, _ in callCount += 1 }
156    given(classMock.setSubscript(42, newValue: true)) ~> { _, _ in callCount += 1 }
157    given(classMock.setSubscript("foo", newValue: "bar")) ~> { _, _ in callCount += 1 }
158    
159    classInstance[42] = "bar"
160    classInstance[42] = true
161    classInstance["foo"] = "bar"
162    
163    verify(classMock.setSubscript(42, newValue: "bar")).wasCalled()
164    verify(classMock.setSubscript(42, newValue: true)).wasCalled()
165    verify(classMock.setSubscript("foo", newValue: "bar")).wasCalled()
166    
167    XCTAssertEqual(callCount, 3)
168  }
169  
170  func testSubscriptClass_handlesMultipleParameterSetter() {
171    var callCount = 0
172    
173    given(classMock.setSubscript(42, 99, newValue: "bar")) ~> { _, _, _ in callCount += 1 }
174    given(classMock.setSubscript("foo", "bar", newValue: "hello")) ~> { _, _ in callCount += 1 }
175    
176    classInstance[42, 99] = "bar"
177    classInstance["foo", "bar"] = "hello"
178    
179    verify(classMock.setSubscript(42, 99, newValue: "bar")).wasCalled()
180    verify(classMock.setSubscript("foo", "bar", newValue: "hello")).wasCalled()
181    
182    XCTAssertEqual(callCount, 2)
183  }
184  
185  func testSubscriptClass_handlesGenericSetter() {
186    enum IndexType { case foo, bar }
187    var callCount = 0
188    
189    given(classMock.setSubscript(IndexType.foo, newValue: IndexType.bar)) ~> { _, _ in
190      callCount += 1
191    }
192    classInstance[IndexType.foo] = IndexType.bar
193    verify(classMock.setSubscript(IndexType.foo, newValue: IndexType.bar)).wasCalled()
194    
195    XCTAssertEqual(callCount, 1)
196  }
197}
198
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)