How to use testSequenceStopsReturningImplementationsWhenReachesEnd method of SequentialValueStubbingTests class

Best Mockingbird code snippet using SequentialValueStubbingTests.testSequenceStopsReturningImplementationsWhenReachesEnd

Run Mockingbird automation tests on LambdaTest cloud grid

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

SequentialValueStubbingTests.swift

Source: SequentialValueStubbingTests.swift Github

copy
1//
2//  SequentialValueStubbingTests.swift
3//  MockingbirdFramework
4//
5//  Created by Andrew Chang on 4/15/20.
6//
7
8import XCTest
9import Mockingbird
10@testable import MockingbirdTestsHost
11
12class SequentialValueStubbingTests: BaseTestCase {
13  
14  var concreteMock: FakeableTypeReferencerMock!
15  var concreteInstance: FakeableTypeReferencer { return concreteMock }
16  
17  override func setUp() {
18    concreteMock = mock(FakeableTypeReferencer.self)
19  }
20  
21  func testValuesReturnedInOrder() {
22    given(concreteMock.fakeableInt()) ~> sequence(of: 1, 2, 3)
23    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
24    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
25    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
26    verify(concreteMock.fakeableInt()).wasCalled(exactly(3))
27  }
28  
29  func testImplementationsReturnedInOrder() {
30    given(concreteMock.fakeableInt()) ~> sequence(of: {1}, {2}, {3})
31    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
32    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
33    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
34    verify(concreteMock.fakeableInt()).wasCalled(exactly(3))
35  }
36  
37  func testImplementationsEvaluatedLazily() {
38    given(concreteMock.fakeableInt()) ~> sequence(of: {1}, { XCTFail("Not lazy"); fatalError() })
39    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
40    verify(concreteMock.fakeableInt()).wasCalled()
41  }
42  
43  func testThrowingImplementationThrows() {
44    let concreteMock = mock(ThrowingProtocol.self)
45    let concreteInstance = concreteMock as ThrowingProtocol
46    struct LazyError: Error {}
47    func generateBool() throws -> Bool { throw LazyError() }
48    given(concreteMock.throwingMethod()) ~> sequence(of: {true}, {try generateBool()})
49    XCTAssertEqual(try concreteInstance.throwingMethod(), true)
50    XCTAssertThrowsError(try concreteInstance.throwingMethod() as Bool)
51  }
52  
53  func testParameterizedImplementation() {
54    let concreteMock = mock(ChildProtocol.self)
55    let concreteInstance = concreteMock as ChildProtocol
56    given(concreteMock.childParameterizedInstanceMethod(param1: any(), any())) ~>
57      sequence(of: { _, _ in
58        return true
59      }, { param1, param2 in
60        return param1 && param2 > 42
61      })
62    XCTAssertTrue(concreteInstance.childParameterizedInstanceMethod(param1: false, 0))
63    XCTAssertTrue(concreteInstance.childParameterizedInstanceMethod(param1: true, 43))
64    XCTAssertFalse(concreteInstance.childParameterizedInstanceMethod(param1: false, 43))
65    XCTAssertFalse(concreteInstance.childParameterizedInstanceMethod(param1: true, 41))
66  }
67  
68  // MARK: Last value sequence
69  
70  func testLastValueUsedWhenSequenceFinished() {
71    given(concreteMock.fakeableInt()) ~> sequence(of: 1, 2, 3)
72    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
73    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
74    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
75    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
76    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
77    verify(concreteMock.fakeableInt()).wasCalled(exactly(5))
78  }
79  
80  func testImplementationUsedWhenSequenceFinished() {
81    given(concreteMock.fakeableInt()) ~> sequence(of: {1}, {2}, {3})
82    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
83    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
84    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
85    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
86    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
87    verify(concreteMock.fakeableInt()).wasCalled(exactly(5))
88  }
89  
90  // MARK: Looping sequence
91  
92  func testSequenceLoopsValuesWhenReachesEnd() {
93    given(concreteMock.fakeableInt()) ~> loopingSequence(of: 1, 2, 3)
94    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
95    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
96    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
97    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
98    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
99    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
100    verify(concreteMock.fakeableInt()).wasCalled(exactly(6))
101  }
102  
103  func testSequenceLoopsImplementationsWhenReachesEnd() {
104    given(concreteMock.fakeableInt()) ~> loopingSequence(of: {1}, {2}, {3})
105    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
106    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
107    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
108    XCTAssertEqual(concreteInstance.fakeableInt(), 1)
109    XCTAssertEqual(concreteInstance.fakeableInt(), 2)
110    XCTAssertEqual(concreteInstance.fakeableInt(), 3)
111    verify(concreteMock.fakeableInt()).wasCalled(exactly(6))
112  }
113  
114  // MARK: Finite sequence
115  
116  func testSequenceStopsReturningValuesWhenReachesEnd() {
117    shouldFail {
118      given(self.concreteMock.fakeableInt()) ~> finiteSequence(of: 1, 2, 3)
119      XCTAssertEqual(self.concreteInstance.fakeableInt(), 1)
120      XCTAssertEqual(self.concreteInstance.fakeableInt(), 2)
121      XCTAssertEqual(self.concreteInstance.fakeableInt(), 3)
122      _ = self.concreteInstance.fakeableInt()
123    }
124  }
125  
126  func testSequenceStopsReturningImplementationsWhenReachesEnd() {
127    shouldFail {
128      given(self.concreteMock.fakeableInt()) ~> finiteSequence(of: {1}, {2}, {3})
129      XCTAssertEqual(self.concreteInstance.fakeableInt(), 1)
130      XCTAssertEqual(self.concreteInstance.fakeableInt(), 2)
131      XCTAssertEqual(self.concreteInstance.fakeableInt(), 3)
132      _ = self.concreteInstance.fakeableInt()
133    }
134  }
135}
136
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 testSequenceStopsReturningImplementationsWhenReachesEnd code on LambdaTest Cloud Grid

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