How to use testPropertyGetterForwardingUnrelatedTypePassesThrough_stubbingOperator method of UnrelatedType class

Best Mockingbird code snippet using UnrelatedType.testPropertyGetterForwardingUnrelatedTypePassesThrough_stubbingOperator

Run Mockingbird automation tests on LambdaTest cloud grid

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

PartialMockTests.swift

Source: PartialMockTests.swift Github

copy
1//
2//  PartialMockTests.swift
3//  MockingbirdTests
4//
5//  Created by typealias on 7/25/21.
6//
7
8import Mockingbird
9import XCTest
10@testable import MockingbirdTestsHost
11
12class PartialMockTests: BaseTestCase {
13  
14  var protocolMock: MinimalProtocolMock!
15  var protocolInstance: MinimalProtocol { return protocolMock }
16  
17  var classMock: MinimalClassMock!
18  var classInstance: MinimalClass { return classMock }
19  
20  class MinimalImplementer: MinimalProtocol {
21    var property: String = "foobar"
22    func method(value: String) -> String {
23      return value
24    }
25  }
26  
27  class OverriddenImplementer: MinimalProtocol {
28    var property: String {
29      get {
30        XCTFail("Property getter should not be called")
31        return ""
32      }
33      set {
34        XCTFail("Property setter should not be called")
35      }
36    }
37    func method(value: String) -> String {
38      XCTFail("Method should not be called")
39      return ""
40    }
41  }
42  
43  class SelfReferencingImplementer: MinimalProtocol {
44    var property: String = "foobar"
45    func method(value: String) -> String {
46      return property
47    }
48  }
49  
50  class UnrelatedType {}
51  
52  override func setUpWithError() throws {
53    protocolMock = mock(MinimalProtocol.self)
54    classMock = mock(MinimalClass.self)
55  }
56  
57  // MARK: - Specific members
58  
59  func testForwardPropertyGetterToObject() throws {
60    given(protocolMock.property).willForward(to: MinimalImplementer())
61    XCTAssertEqual(protocolMock.property, "foobar")
62    protocolMock.property = "hello"
63    XCTAssertEqual(protocolMock.property, "foobar") // Setter is not stubbed
64    verify(protocolMock.property).wasCalled(twice)
65  }
66  func testForwardPropertyGetterToObject_stubbingOperator() throws {
67    given(protocolMock.property) ~> forward(to: MinimalImplementer())
68    XCTAssertEqual(protocolMock.property, "foobar")
69    protocolMock.property = "hello"
70    XCTAssertEqual(protocolMock.property, "foobar") // Setter is not stubbed
71    verify(protocolMock.property).wasCalled(twice)
72  }
73  
74  func testForwardPropertySetterToObject() throws {
75    let implementer = MinimalImplementer()
76    given(protocolMock.property = firstArg(any())).willForward(to: implementer)
77    protocolMock.property = "hello"
78    XCTAssertEqual(implementer.property, "hello")
79    verify(protocolMock.property = "hello").wasCalled()
80  }
81  func testForwardPropertySetterToObject_stubbingOperator() throws {
82    let implementer = MinimalImplementer()
83    given(protocolMock.property = firstArg(any())) ~> forward(to: implementer)
84    protocolMock.property = "hello"
85    XCTAssertEqual(implementer.property, "hello")
86    verify(protocolMock.property = "hello").wasCalled()
87  }
88  
89  func testForwardMethodToObject() throws {
90    given(protocolMock.method(value: any())).willForward(to: MinimalImplementer())
91    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
92    verify(protocolMock.method(value: "hello")).wasCalled()
93  }
94  func testForwardMethodToObject_stubbingOperator() throws {
95    given(protocolMock.method(value: any())) ~> forward(to: MinimalImplementer())
96    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
97    verify(protocolMock.method(value: "hello")).wasCalled()
98  }
99  
100  func testForwardPropertyToSuperclass() throws {
101    given(classMock.property).willForwardToSuper()
102    given(classMock.property = firstArg(any())).willForwardToSuper()
103    XCTAssertEqual(classMock.property, "super")
104    classMock.property = "hello"
105    XCTAssertEqual(classMock.property, "hello")
106    verify(classMock.property = firstArg(any())).wasCalled()
107  }
108  func testForwardPropertyToSuperclass_stubbingOperator() throws {
109    given(classMock.property) ~> forwardToSuper()
110    given(classMock.property = firstArg(any())) ~> forwardToSuper()
111    XCTAssertEqual(classMock.property, "super")
112    classMock.property = "hello"
113    XCTAssertEqual(classMock.property, "hello")
114    verify(classMock.property = firstArg(any())).wasCalled()
115  }
116  
117  func testForwardMethodToSuperclass() throws {
118    given(classMock.property).willReturn("world")
119    given(classMock.method(value: any())).willForwardToSuper()
120    XCTAssertEqual(classMock.method(value: "hello"), "hello-world")
121    verify(classMock.property).wasCalled()
122    verify(classMock.method(value: "hello")).wasCalled()
123  }
124  func testForwardMethodToSuperclass_stubbingOperator() throws {
125    given(classMock.property) ~> "world"
126    given(classMock.method(value: any())) ~> forwardToSuper()
127    XCTAssertEqual(classMock.method(value: "hello"), "hello-world")
128    verify(classMock.property).wasCalled()
129    verify(classMock.method(value: "hello")).wasCalled()
130  }
131  
132  // MARK: - Precedence
133  
134  func testPropertyGetterForwardingPrecedence() throws {
135    given(protocolMock.property).willForward(to: OverriddenImplementer())
136    given(protocolMock.property).willForward(to: MinimalImplementer())
137    XCTAssertEqual(protocolMock.property, "foobar")
138  }
139  func testPropertyGetterForwardingPrecedence_stubbingOperator() throws {
140    given(protocolMock.property) ~> forward(to: OverriddenImplementer())
141    given(protocolMock.property) ~> forward(to: MinimalImplementer())
142    XCTAssertEqual(protocolMock.property, "foobar")
143  }
144  
145  func testPropertyGetterForwardingPrecedenceWithExplicitStubs() throws {
146    given(protocolMock.property).willForward(to: OverriddenImplementer())
147    given(protocolMock.property).willReturn("hello")
148    XCTAssertEqual(protocolMock.property, "hello")
149  }
150  func testPropertyGetterForwardingPrecedenceWithExplicitStubs_stubbingOperator() throws {
151    given(protocolMock.property) ~> forward(to: OverriddenImplementer())
152    given(protocolMock.property) ~> "hello"
153    XCTAssertEqual(protocolMock.property, "hello")
154  }
155  
156  func testPropertySetterForwardingPrecedence() throws {
157    given(protocolMock.property = firstArg(any())).willForward(to: OverriddenImplementer())
158    given(protocolMock.property = firstArg(any())).willForward(to: MinimalImplementer())
159    protocolMock.property = "foobar"
160  }
161  func testPropertySetterForwardingPrecedence_stubbingOperator() throws {
162    given(protocolMock.property = firstArg(any())) ~> forward(to: OverriddenImplementer())
163    given(protocolMock.property = firstArg(any())) ~> forward(to: MinimalImplementer())
164    protocolMock.property = "foobar"
165  }
166  
167  func testPropertySetterForwardingPrecedenceWithExplicitStubs() throws {
168    given(protocolMock.property = firstArg(any())).willForward(to: OverriddenImplementer())
169    let expectation = XCTestExpectation()
170    given(protocolMock.property = "foobar").will { expectation.fulfill() }
171    protocolMock.property = "foobar"
172    wait(for: [expectation], timeout: 2)
173  }
174  func testPropertySetterForwardingPrecedenceWithExplicitStubs_stubbingOperator() throws {
175    given(protocolMock.property = firstArg(any())) ~> forward(to: OverriddenImplementer())
176    let expectation = XCTestExpectation()
177    given(protocolMock.property = "foobar") ~> { expectation.fulfill() }
178    protocolMock.property = "foobar"
179    wait(for: [expectation], timeout: 2)
180  }
181  
182  func testMethodForwardingPrecedence() throws {
183    given(protocolMock.method(value: any())).willForward(to: OverriddenImplementer())
184    given(protocolMock.method(value: any())).willForward(to: MinimalImplementer())
185    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
186  }
187  func testMethodForwardingPrecedence_stubbingOperator() throws {
188    given(protocolMock.method(value: any())) ~> forward(to: OverriddenImplementer())
189    given(protocolMock.method(value: any())) ~> forward(to: MinimalImplementer())
190    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
191  }
192  
193  func testMethodForwardingPrecedenceWithExplicitStubs() throws {
194    given(protocolMock.method(value: any())).willForward(to: OverriddenImplementer())
195    given(protocolMock.method(value: any())).willReturn("hello")
196    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
197  }
198  func testMethodForwardingPrecedenceWithExplicitStubs_stubbingOperator() throws {
199    given(protocolMock.method(value: any())) ~> forward(to: OverriddenImplementer())
200    given(protocolMock.method(value: any())) ~> "hello"
201    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
202  }
203  
204  // MARK: - Global
205  
206  func testForwardAllPropertiesToObject() throws {
207    let implementer = MinimalImplementer()
208    protocolMock.forwardCalls(to: implementer)
209    XCTAssertEqual(protocolMock.property, "foobar")
210    protocolMock.property = "hello"
211    XCTAssertEqual(protocolMock.property, "hello")
212    XCTAssertEqual(implementer.property, "hello")
213    verify(protocolMock.property).wasCalled(twice)
214  }
215  
216  func testForwardAllMethodsToObject() throws {
217    protocolMock.forwardCalls(to: MinimalImplementer())
218    XCTAssertEqual(protocolMock.method(value: "hello"), "hello")
219    verify(protocolMock.method(value: "hello")).wasCalled()
220  }
221  
222  func testForwardAllMethodsPrecedence() throws {
223    protocolMock.forwardCalls(to: OverriddenImplementer())
224    protocolMock.forwardCalls(to: MinimalImplementer())
225    XCTAssertEqual(protocolMock.property, "foobar")
226  }
227  
228  // MARK: - API misuse
229  
230  func testPropertyGetterForwardingUnrelatedTypeFails() throws {
231    shouldFail {
232      given(self.protocolMock.property).willForward(to: "foobar")
233      _ = self.protocolMock.property
234    }
235  }
236  func testPropertyGetterForwardingUnrelatedTypeFails_stubbingOperator() throws {
237    shouldFail {
238      given(self.protocolMock.property) ~> forward(to: "foobar")
239      _ = self.protocolMock.property
240    }
241  }
242  
243  func testPropertyGetterForwardingUnrelatedTypePassesThrough() throws {
244    given(protocolMock.property).willForward(to: MinimalImplementer())
245    given(protocolMock.property).willForward(to: UnrelatedType())
246    XCTAssertEqual(protocolMock.property, "foobar")
247  }
248  func testPropertyGetterForwardingUnrelatedTypePassesThrough_stubbingOperator() throws {
249    given(protocolMock.property) ~> forward(to: MinimalImplementer())
250    given(protocolMock.property) ~> forward(to: UnrelatedType())
251    XCTAssertEqual(protocolMock.property, "foobar")
252  }
253  
254  func testPropertySetterForwardingUnrelatedTypePassesThrough() throws {
255    let implementer = MinimalImplementer()
256    given(protocolMock.property = "foobar").willForward(to: implementer)
257    given(protocolMock.property = "foobar").willForward(to: UnrelatedType())
258    XCTAssertEqual(implementer.property, "foobar")
259  }
260  func testPropertySetterForwardingUnrelatedTypePassesThrough_stubbingOperator() throws {
261    let implementer = MinimalImplementer()
262    given(protocolMock.property = "foobar") ~> forward(to: implementer)
263    given(protocolMock.property = "foobar") ~> forward(to: UnrelatedType())
264    XCTAssertEqual(implementer.property, "foobar")
265  }
266  
267  func testMethodForwardingUnrelatedTypePassesThrough() throws {
268    given(protocolMock.method(value: any())).willForward(to: MinimalImplementer())
269    given(protocolMock.method(value: any())).willForward(to: UnrelatedType())
270    XCTAssertEqual(protocolMock.method(value: "foobar"), "foobar")
271  }
272  func testMethodForwardingUnrelatedTypePassesThrough_stubbingOperator() throws {
273    given(protocolMock.method(value: any())) ~> forward(to: MinimalImplementer())
274    given(protocolMock.method(value: any())) ~> forward(to: UnrelatedType())
275    XCTAssertEqual(protocolMock.method(value: "foobar"), "foobar")
276  }
277  
278}
279
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 UnrelatedType

Trigger testPropertyGetterForwardingUnrelatedTypePassesThrough_stubbingOperator code on LambdaTest Cloud Grid

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