How to use testGetterReturnsValue method of InlinePropertyTests class

Best Mockingbird code snippet using InlinePropertyTests.testGetterReturnsValue

Run Mockingbird automation tests on LambdaTest cloud grid

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

InlinePropertyTests.swift

Source: InlinePropertyTests.swift Github

copy
1//
2//  InlinePropertyTests.swift
3//  MockingbirdTests
4//
5//  Created by typealias on 7/25/21.
6//
7
8import Mockingbird
9import XCTest
10@testable import MockingbirdTestsHost
11
12class InlinePropertyTests: BaseTestCase {
13  
14  var protocolMock: MinimalProtocolMock!
15  var protocolInstance: MinimalProtocol { return protocolMock }
16  
17  override func setUpWithError() throws {
18    protocolMock = mock(MinimalProtocol.self)
19  }
20  
21  // MARK: - Getter
22  
23  func testGetterReturnsValue() throws {
24    given(protocolMock.property).willReturn("hello")
25    XCTAssertEqual(protocolMock.property, "hello")
26    verify(protocolMock.property).wasCalled()
27  }
28  func testGetterReturnsValue_stubbingOperator() throws {
29    given(protocolMock.property) ~> "hello"
30    XCTAssertEqual(protocolMock.property, "hello")
31    verify(protocolMock.property).wasCalled()
32  }
33  
34  func testGetterCallsImplementation() throws {
35    given(protocolMock.property).will { return "hello" }
36    XCTAssertEqual(protocolMock.property, "hello")
37    verify(protocolMock.property).wasCalled()
38  }
39  func testGetterCallsImplementation_stubbingOperator() throws {
40    given(protocolMock.property) ~> { return "hello" }
41    XCTAssertEqual(protocolMock.property, "hello")
42    verify(protocolMock.property).wasCalled()
43  }
44  
45  func testGetterIgnoresThrowingErrorStub() throws {
46    shouldFail {
47      struct FakeError: Error {}
48      given(self.protocolMock.property).willThrow(FakeError())
49      _ = self.protocolMock.property
50    }
51  }
52  func testGetterIgnoresThrowingErrorStub_stubbingOperator() throws {
53    shouldFail {
54      struct FakeError: Error {}
55      given(self.protocolMock.property) ~> { throw FakeError() }
56      _ = self.protocolMock.property
57    }
58  }
59  
60  // MARK: - Setter
61  
62  func testSetterExactMatchCallsImplementation() throws {
63    let expectation = XCTestExpectation()
64    given(protocolMock.property = "hello").will { expectation.fulfill() }
65    protocolMock.property = "hello"
66    verify(protocolMock.property = "hello").wasCalled()
67    wait(for: [expectation], timeout: 2)
68  }
69  func testSetterExactMatchCallsImplementation_stubbingOperator() throws {
70    let expectation = XCTestExpectation()
71    given(protocolMock.property = "hello") ~> { expectation.fulfill() }
72    protocolMock.property = "hello"
73    verify(protocolMock.property = "hello").wasCalled()
74    wait(for: [expectation], timeout: 2)
75  }
76  
77  func testSetterExactMatchIgnoresThrowingErrorStub() throws {
78    struct FakeError: Error {}
79    given(self.protocolMock.property = "hello").willThrow(FakeError())
80    self.protocolMock.property = "hello"
81  }
82  func testSetterExactMatchIgnoresThrowingErrorStub_stubbingOperator() throws {
83    struct FakeError: Error {}
84    given(self.protocolMock.property = "hello") ~> { throw FakeError() }
85    self.protocolMock.property = "hello"
86  }
87  
88  func testSetterWildcardMatchCallsImplementation() throws {
89    let expectation = XCTestExpectation()
90    expectation.expectedFulfillmentCount = 2
91    given(protocolMock.property = any()).will { expectation.fulfill() }
92    protocolMock.property = "hello"
93    protocolMock.property = "goodbye"
94    verify(protocolMock.property = any()).wasCalled(twice)
95    wait(for: [expectation], timeout: 2)
96  }
97  func testSetterWildcardMatchCallsImplementation_stubbingOperator() throws {
98    let expectation = XCTestExpectation()
99    expectation.expectedFulfillmentCount = 2
100    given(protocolMock.property = any()) ~> { expectation.fulfill() }
101    protocolMock.property = "hello"
102    protocolMock.property = "goodbye"
103    verify(protocolMock.property = any()).wasCalled(twice)
104    wait(for: [expectation], timeout: 2)
105  }
106  
107  func testSetterWildcardMatchWithExplicitIndexCallsImplementation() throws {
108    let expectation = XCTestExpectation()
109    expectation.expectedFulfillmentCount = 2
110    given(protocolMock.property = firstArg(any())).will { expectation.fulfill() }
111    protocolMock.property = "hello"
112    protocolMock.property = "goodbye"
113    verify(protocolMock.property = firstArg(any())).wasCalled(twice)
114    wait(for: [expectation], timeout: 2)
115  }
116  func testSetterWildcardMatchWithExplicitIndexCallsImplementation_stubbingOperator() throws {
117    let expectation = XCTestExpectation()
118    expectation.expectedFulfillmentCount = 2
119    given(protocolMock.property = firstArg(any())) ~> { expectation.fulfill() }
120    protocolMock.property = "hello"
121    protocolMock.property = "goodbye"
122    verify(protocolMock.property = firstArg(any())).wasCalled(twice)
123    wait(for: [expectation], timeout: 2)
124  }
125  
126  
127  func testSetterConditionalWildcardMatchCallsImplementation() throws {
128    let expectation = XCTestExpectation()
129    expectation.expectedFulfillmentCount = 2
130    given(protocolMock.property = any(where: { $0.first == "h" })).will { expectation.fulfill() }
131    protocolMock.property = "hello"
132    protocolMock.property = "hey"
133    verify(protocolMock.property = any(where: { $0.first == "h" })).wasCalled(twice)
134    wait(for: [expectation], timeout: 2)
135  }
136  func testSetterConditionalWildcardMatchCallsImplementation_stubbingOperator() throws {
137    let expectation = XCTestExpectation()
138    expectation.expectedFulfillmentCount = 2
139    given(protocolMock.property = any(where: { $0.first == "h" })) ~> { expectation.fulfill() }
140    protocolMock.property = "hello"
141    protocolMock.property = "hey"
142    verify(protocolMock.property = any(where: { $0.first == "h" })).wasCalled(twice)
143    wait(for: [expectation], timeout: 2)
144  }
145  
146  func testSetterConditionalWildcardMatchDoesNotCallImplementation() throws {
147    given(protocolMock.property = any(where: { $0.count == 10 })).will { XCTFail() }
148    protocolMock.property = "hello"
149    verify(protocolMock.property = any(where: { $0.count == 10 })).wasNeverCalled()
150  }
151  func testSetterConditionalWildcardMatchDoesNotCallImplementation_stubbingOperator() throws {
152    given(protocolMock.property = any(where: { $0.count == 10 })) ~> { XCTFail() }
153    protocolMock.property = "hello"
154    verify(protocolMock.property = any(where: { $0.count == 10 })).wasNeverCalled()
155  }
156  
157  // MARK: - Precendence
158  
159  func testGetterLaterStubsHavePrecendence() throws {
160    given(protocolMock.property).willReturn("1")
161    XCTAssertEqual(protocolMock.property, "1")
162    given(protocolMock.property).willReturn("2")
163    XCTAssertEqual(protocolMock.property, "2")
164    verify(protocolMock.property).wasCalled(twice)
165  }
166  func testGetterLaterStubsHavePrecendence_stubbingOperator() throws {
167    given(protocolMock.property) ~> "1"
168    XCTAssertEqual(protocolMock.property, "1")
169    given(protocolMock.property) ~> "2"
170    XCTAssertEqual(protocolMock.property, "2")
171    verify(protocolMock.property).wasCalled(twice)
172  }
173  
174  func testSetterExactMatchLaterStubsHavePrecendence() throws {
175    let expectation = XCTestExpectation()
176    given(protocolMock.property = "1").will { XCTFail() }
177    given(protocolMock.property = "1").will { expectation.fulfill() }
178    protocolMock.property = "1"
179    verify(protocolMock.property = "1").wasCalled()
180    wait(for: [expectation], timeout: 2)
181  }
182  func testSetterExactMatchLaterStubsHavePrecendence_stubbingOperator() throws {
183    let expectation = XCTestExpectation()
184    given(protocolMock.property = "1") ~> { XCTFail() }
185    given(protocolMock.property = "1") ~> { expectation.fulfill() }
186    protocolMock.property = "1"
187    verify(protocolMock.property = "1").wasCalled()
188    wait(for: [expectation], timeout: 2)
189  }
190  
191  func testSetterWildcardMatchLaterStubsHavePrecendence() throws {
192    let expectation = XCTestExpectation()
193    expectation.expectedFulfillmentCount = 2
194    given(protocolMock.property = firstArg(any())).will { XCTFail() }
195    given(protocolMock.property = firstArg(any())).will { expectation.fulfill() }
196    protocolMock.property = "1"
197    protocolMock.property = "2"
198    verify(protocolMock.property = firstArg(any())).wasCalled(twice)
199    wait(for: [expectation], timeout: 2)
200  }
201  func testSetterWildcardMatchLaterStubsHavePrecendence_stubbingOperator() throws {
202    let expectation = XCTestExpectation()
203    expectation.expectedFulfillmentCount = 2
204    given(protocolMock.property = firstArg(any())) ~> { XCTFail() }
205    given(protocolMock.property = firstArg(any())) ~> { expectation.fulfill() }
206    protocolMock.property = "1"
207    protocolMock.property = "2"
208    verify(protocolMock.property = firstArg(any())).wasCalled(twice)
209    wait(for: [expectation], timeout: 2)
210  }
211  
212}
213
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

Trigger testGetterReturnsValue code on LambdaTest Cloud Grid

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