How to use ExpectationGroup class

Best Mockingbird code snippet using ExpectationGroup

OrderedVerification.swift

Source:OrderedVerification.swift Github

copy

Full Screen

...149 baseInvocation: capturedExpectation.invocation,150 unexpectedInvocations: unexpectedInvocations,151 priorToBase: true152 )153 throw ExpectationGroup.Failure(error: failure,154 sourceLocation: capturedExpectation.expectation.sourceLocation)155 }156}157private func assertNoInvocationsAfter(_ capturedExpectation: CapturedExpectation,158 baseInvocation: Invocation?,159 contexts: [UUID: MockingContext]) throws {160 let allInvocations = getAllInvocations(in: contexts, after: baseInvocation)161 guard !allInvocations.isEmpty else { return }162 163 let failure = TestFailure.unexpectedInvocations(164 baseInvocation: capturedExpectation.invocation,165 unexpectedInvocations: allInvocations,166 priorToBase: false167 )168 throw ExpectationGroup.Failure(error: failure,169 sourceLocation: capturedExpectation.expectation.sourceLocation)170}171private struct Solution {172 let firstInvocation: Invocation?173 let lastInvocation: Invocation?174 175 enum Failure: Error {176 case unsatisfiable177 }178}179private func satisfy(_ capturedExpectations: [CapturedExpectation],180 at index: Int = 0,181 baseInvocation: Invocation? = nil,182 contexts: [UUID: MockingContext],183 options: OrderedVerificationOptions) throws -> Solution {184 let capturedExpectation = capturedExpectations[index]185 let allInvocations = findInvocations(in: capturedExpectation.mockingContext,186 with: capturedExpectation.invocation.selectorName,187 before: nil,188 after: baseInvocation)189 var nextInvocationIndex = 1190 191 while true {192 if options.contains(.onlyConsecutiveInvocations), baseInvocation != nil {193 try assertNoInvocationsBefore(capturedExpectation,194 baseInvocation: baseInvocation,195 contexts: contexts)196 }197 do {198 // Try to satisfy the current expectations.199 let allInvocations = try expect(capturedExpectation.mockingContext,200 handled: capturedExpectation.invocation,201 using: capturedExpectation.expectation,202 before: allInvocations.get(nextInvocationIndex),203 after: baseInvocation)204 guard index+1 < capturedExpectations.count else { // Found a solution!205 return Solution(firstInvocation: allInvocations.first, lastInvocation: allInvocations.last)206 }207 208 // Potential match with the current base invocation, try satisfying the next expectation.209 let allMatchingInvocations = allInvocations.filter({ $0.isEqual(to: capturedExpectation.invocation) })210 let result = try satisfy(capturedExpectations,211 at: index+1,212 baseInvocation: allMatchingInvocations.first,213 contexts: contexts,214 options: options)215 216 // Check if still satisfiable when using the next invocation as a constraint.217 if let nextBaseInvocation = result.firstInvocation {218 try expect(capturedExpectation.mockingContext,219 handled: capturedExpectation.invocation,220 using: capturedExpectation.expectation,221 before: nextBaseInvocation,222 after: baseInvocation)223 }224 225 return Solution(firstInvocation: allInvocations.first, lastInvocation: result.lastInvocation)226 } catch let error as ExpectationGroup.Failure { // Propagate the precondition failure.227 throw error228 } catch { // Unable to satisfy the current expectation.229 guard nextInvocationIndex+1 <= allInvocations.count else {230 throw Solution.Failure.unsatisfiable // Unable to grow the window further.231 }232 nextInvocationIndex += 1 // Grow the invocation window.233 }234 }235}236/// Internal helper for `inOrder` verification scopes.237/// 1. Creates an attributed `DispatchQueue` scope which collects all verifications.238/// 2. Checks invocations on each mock using the provided `options`.239func createOrderedContext(at sourceLocation: SourceLocation,240 options: OrderedVerificationOptions,241 block scope: () -> Void) {242 let group = ExpectationGroup { group in243 let contexts = group.expectations.reduce(into: [UUID: MockingContext]()) {244 (result, expectation) in245 result[expectation.mockingContext.identifier] = expectation.mockingContext246 }247 248 // Check for invocations prior to the first expectation's invocation(s).249 if options.contains(.noInvocationsBefore), let firstExpectation = group.expectations.first {250 try assertNoInvocationsBefore(firstExpectation, baseInvocation: nil, contexts: contexts)251 }252 253 do {254 let result = try satisfy(group.expectations, contexts: contexts, options: options)255 256 // Check for invocations after the last expectation's invocation(s).257 if options.contains(.noInvocationsAfter), let lastExpectation = group.expectations.last {258 try assertNoInvocationsAfter(lastExpectation,259 baseInvocation: result.lastInvocation,260 contexts: contexts)261 }262 } catch let error as ExpectationGroup.Failure {263 throw error // Propagate wrapped precondition error.264 } catch _ as Solution.Failure {265 // It's difficult to clearly determine which expectation is breaking the group, so instead266 // just throw an error at the group level instead.267 let allInvocations = getAllInvocations(in: contexts, after: nil)268 let failure = TestFailure.unsatisfiableExpectations(capturedExpectations: group.expectations,269 allInvocations: allInvocations)270 throw ExpectationGroup.Failure(error: failure, sourceLocation: sourceLocation)271 } catch {272 fatalError("Unexpected error type") // This shouldn't happen.273 }274 }275 276 let queue = DispatchQueue(label: "co.bird.mockingbird.verify.inOrder")277 queue.setSpecific(key: ExpectationGroup.contextKey, value: group)278 queue.sync { scope() }279 280 do {281 try group.verify()282 } catch let error as ExpectationGroup.Failure {283 FailTest(String(describing: error),284 file: error.sourceLocation.file,285 line: error.sourceLocation.line)286 } catch {287 fatalError("Unexpected error type") // This shouldn't happen.288 }289}...

Full Screen

Full Screen

ExpectationGroup.swift

Source:ExpectationGroup.swift Github

copy

Full Screen

1//2// ExpectationGroup.swift3// MockingbirdFramework4//5// Created by Andrew Chang on 5/31/20.6//7import Foundation8/// A deferred expectation that can be fulfilled when an invocation arrives later.9struct CapturedExpectation {10 let mockingContext: MockingContext11 let invocation: Invocation12 let expectation: Expectation13}14/// Stores all expectations invoked by verification methods within a scoped context.15class ExpectationGroup {16 static let contextKey = DispatchSpecificKey<ExpectationGroup>()17 18 private(set) weak var parent: ExpectationGroup?19 private let verificationBlock: (ExpectationGroup) throws -> Void20 21 init(_ verificationBlock: @escaping (ExpectationGroup) throws -> Void) {22 self.parent = DispatchQueue.currentExpectationGroup23 self.verificationBlock = verificationBlock24 }25 26 struct Failure: Error {27 let error: TestFailure28 let sourceLocation: SourceLocation29 }30 func verify(context: ExpectationGroup? = nil) throws {31 if let parent = parent, context == nil {32 parent.addSubgroup(self)33 } else {34 try verificationBlock(self)35 }36 }37 38 private(set) var expectations = [CapturedExpectation]()39 func addExpectation(mockingContext: MockingContext,40 invocation: Invocation,41 expectation: Expectation) {42 expectations.append(CapturedExpectation(mockingContext: mockingContext,43 invocation: invocation,44 expectation: expectation))45 }46 47 private(set) var subgroups = [ExpectationGroup]()48 func addSubgroup(_ subgroup: ExpectationGroup) {49 subgroups.append(subgroup)50 }51}52extension DispatchQueue {53 class var currentExpectationGroup: ExpectationGroup? {54 return DispatchQueue.getSpecific(key: ExpectationGroup.contextKey)55 }56}...

Full Screen

Full Screen

ExpectationGroup

Using AI Code Generation

copy

Full Screen

1import Mockingbird2import XCTest3class ExpectationGroupTests: XCTestCase {4 override func setUp() {5 super.setUp()6 expectationGroup = ExpectationGroup()7 }8 func testExpectationGroup() {9 let order = expectationGroup.expectation("order")10 let payment = expectationGroup.expectation("payment")11 let delivery = expectationGroup.expectation("delivery")12 expectationGroup.notify(on: DispatchQueue.main) {13 print("All tasks done")14 }15 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {16 order.fulfill()17 }18 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {19 payment.fulfill()20 }21 DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {22 delivery.fulfill()23 }24 wait(for: [expectationGroup.expectation], timeout: 1)25 }26}27import Mockingbird28import XCTest29class ExpectationGroupTests: XCTestCase {30 override func setUp() {31 super.setUp()32 expectationGroup = ExpectationGroup()33 }34 func testExpectationGroup() {35 let order = expectationGroup.expectation("order")36 let payment = expectationGroup.expectation("payment")37 let delivery = expectationGroup.expectation("delivery")38 expectationGroup.notify(on: DispatchQueue.main) {39 print("All tasks done")40 }41 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {42 order.fulfill()43 }44 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {45 payment.fulfill()46 }47 DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {48 delivery.fulfill()49 }50 wait(for: [expectationGroup.expectation], timeout: 1)51 }52}

Full Screen

Full Screen

ExpectationGroup

Using AI Code Generation

copy

Full Screen

1import Mockingbird2let expectationGroup = ExpectationGroup()3let expectation = expectationGroup.createExpectation()4let expectationGroup = XCTestExpectation(description: "test")5let expectation = expectationGroup.expectation(description: "test")6import Mockingbird7let expectationGroup = ExpectationGroup()8let expectation = expectationGroup.createExpectation()9let expectationGroup = XCTestExpectation(description: "test")10let expectation = expectationGroup.expectation(description: "test")11import Mockingbird12let expectationGroup = ExpectationGroup()13let expectation = expectationGroup.createExpectation()14let expectationGroup = XCTestExpectation(description: "test")15let expectation = expectationGroup.expectation(description: "test")16import Mockingbird17let expectationGroup = ExpectationGroup()18let expectation = expectationGroup.createExpectation()19let expectationGroup = XCTestExpectation(description: "test")20let expectation = expectationGroup.expectation(description: "test")21import Mockingbird22let expectationGroup = ExpectationGroup()23let expectation = expectationGroup.createExpectation()24let expectationGroup = XCTestExpectation(description: "test")25let expectation = expectationGroup.expectation(description: "test")26import Mockingbird27let expectationGroup = ExpectationGroup()28let expectation = expectationGroup.createExpectation()29let expectationGroup = XCTestExpectation(description: "test")30let expectation = expectationGroup.expectation(description: "test")31import Mockingbird32let expectationGroup = ExpectationGroup()33let expectation = expectationGroup.createExpectation()

Full Screen

Full Screen

ExpectationGroup

Using AI Code Generation

copy

Full Screen

1import Mockingbird2let group = ExpectationGroup()3let mock = mock(MyProtocol.self, named: "mock", in: group)4when(mock.method()).thenDoNothing()5verify(mock.method()).wasCalled(in: group)6import Mockingbird7let mock = mock(MyProtocol.self)8when(mock.method()).thenDoNothing()9when(mock.method()).thenThrow(MyError())10when(mock.method()).thenReturn("value")11when(mock.method()).then { _ in "value" }12when(mock.method()).thenCallRealImplementation()13##### `thenDoNothing()`14import Mockingbird15let mock = mock(MyProtocol.self)16when(mock.method()).thenDoNothing()17##### `thenThrow(_:)`18import Mockingbird19let mock = mock(MyProtocol.self)20when(mock.method()).thenThrow(MyError())21##### `thenReturn(_:)`22import Mockingbird23let mock = mock(MyProtocol.self)24when(mock.method()).thenReturn("value")25##### `then(_:)`26import Mocking

Full Screen

Full Screen

ExpectationGroup

Using AI Code Generation

copy

Full Screen

1import XCTest2import Mockingbird3import MockingbirdXCTest4import MyFramework5class ExpectationGroupTest: XCTestCase {6 func testExpectationGroup() {7 let mockMyFramework = mock(MyFramework.self)8 let myExpectationGroup = ExpectationGroup()9 myExpectationGroup.setExpectation()10 given(mockMyFramework.myFunction()) ~> { _ in11 myExpectationGroup.wait()12 }13 XCTAssertEqual(myFramework.myFunction(), "myFunction")14 myExpectationGroup.verify()15 myExpectationGroup.setExpectation()16 given(mockMyFramework.myFunction()) ~> { _ in17 myExpectationGroup.wait()18 }19 XCTAssertEqual(myFramework.myFunction(), "myFunction")20 myExpectationGroup.verify()21 }22}

Full Screen

Full Screen

ExpectationGroup

Using AI Code Generation

copy

Full Screen

1import XCTest2import Mockingbird3@testable import MockingbirdTestsHost4class ExpectationGroupTests: XCTestCase {5 override func setUp() {6 super.setUp()7 mock = mock(ExpectationGroupProtocol.self)8 group = ExpectationGroup()9 expectation = XCTestExpectation(description: "ExpectationGroupTests")10 }11 override func tearDown() {12 super.tearDown()13 }14 func testExpectationGroup() {15 let expectation1 = group.expectation(description: "Expectation 1")16 let expectation2 = group.expectation(description: "Expectation 2")17 let expectation3 = group.expectation(description: "Expectation 3")18 given(mock.doSomethingWithCompletionHandler(any())) ~> { (completion: @escaping () -> Void) in19 completion()20 }21 mock.doSomethingWithCompletionHandler {22 self.group.notify {23 XCTAssertTrue(self.group.isFulfilled)24 expectation1.fulfill()25 expectation2.fulfill()26 expectation3.fulfill()27 }28 }29 wait(for: [expectation1, expectation2, expectation3], timeout: 1)30 }31}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockingbird automation tests on LambdaTest cloud grid

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

Most used methods in ExpectationGroup

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful