How to use AssertionRecorder class

Best Nimble code snippet using .AssertionRecorder

Run Nimble automation tests on LambdaTest cloud grid

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

AssertionRecorder.swift

Source: AssertionRecorder.swift Github

copy
1import Foundation
2
3/// A data structure that stores information about an assertion when
4/// AssertionRecorder is set as the Nimble assertion handler.
5///
6/// @see AssertionRecorder
7/// @see AssertionHandler
8public struct AssertionRecord: CustomStringConvertible {
9    /// Whether the assertion succeeded or failed
10    public let success: Bool
11    /// The failure message the assertion would display on failure.
12    public let message: FailureMessage
13    /// The source location the expectation occurred on.
14    public let location: SourceLocation
15
16    public var description: String {
17        return "AssertionRecord { success=\(success), message='\(message.stringValue)', location=\(location) }"
18    }
19}
20
21/// An AssertionHandler that silently records assertions that Nimble makes.
22/// This is useful for testing failure messages for matchers.
23///
24/// @see AssertionHandler
25public class AssertionRecorder: AssertionHandler {
26    /// All the assertions that were captured by this recorder
27    public var assertions = [AssertionRecord]()
28
29    public init() {}
30
31    public func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
32        assertions.append(
33            AssertionRecord(
34                success: assertion,
35                message: message,
36                location: location))
37    }
38}
39
40/// Allows you to temporarily replace the current Nimble assertion handler with
41/// the one provided for the scope of the closure.
42///
43/// Once the closure finishes, then the original Nimble assertion handler is restored.
44///
45/// @see AssertionHandler
46public func withAssertionHandler(_ tempAssertionHandler: AssertionHandler, closure: () throws -> Void) {
47    let environment = NimbleEnvironment.activeInstance
48    let oldRecorder = environment.assertionHandler
49    let capturer = NMBExceptionCapture(handler: nil, finally: ({
50        environment.assertionHandler = oldRecorder
51    }))
52    environment.assertionHandler = tempAssertionHandler
53    capturer.tryBlock {
54        try! closure()
55    }
56}
57
58/// Captures expectations that occur in the given closure. Note that all
59/// expectations will still go through to the default Nimble handler.
60///
61/// This can be useful if you want to gather information about expectations
62/// that occur within a closure.
63///
64/// @param silently expectations are no longer send to the default Nimble 
65///                 assertion handler when this is true. Defaults to false.
66///
67/// @see gatherFailingExpectations
68public func gatherExpectations(silently: Bool = false, closure: @escaping () -> Void) -> [AssertionRecord] {
69    let previousRecorder = NimbleEnvironment.activeInstance.assertionHandler
70    let recorder = AssertionRecorder()
71    let handlers: [AssertionHandler]
72
73    if silently {
74        handlers = [recorder]
75    } else {
76        handlers = [recorder, previousRecorder]
77    }
78
79    let dispatcher = AssertionDispatcher(handlers: handlers)
80    withAssertionHandler(dispatcher, closure: closure)
81    return recorder.assertions
82}
83
84/// Captures failed expectations that occur in the given closure. Note that all
85/// expectations will still go through to the default Nimble handler.
86///
87/// This can be useful if you want to gather information about failed
88/// expectations that occur within a closure.
89///
90/// @param silently expectations are no longer send to the default Nimble
91///                 assertion handler when this is true. Defaults to false.
92///
93/// @see gatherExpectations
94/// @see raiseException source for an example use case.
95public func gatherFailingExpectations(silently: Bool = false, closure: @escaping () -> Void) -> [AssertionRecord] {
96    let assertions = gatherExpectations(silently: silently, closure: closure)
97    return assertions.filter { assertion in
98        !assertion.success
99    }
100}
101
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 methods in AssertionRecorder

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)