How to use assert method of NimbleXCTestUnavailableHandler class

Best Nimble code snippet using NimbleXCTestUnavailableHandler.assert

Run Nimble automation tests on LambdaTest cloud grid

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

AdapterProtocols.swift

Source: AdapterProtocols.swift Github

copy
1import Foundation
2
3/// Protocol for the assertion handler that Nimble uses for all expectations.
4public protocol AssertionHandler {
5    func assert(assertion: Bool, message: FailureMessage, location: SourceLocation)
6}
7
8/// Global backing interface for assertions that Nimble creates.
9/// Defaults to a private test handler that passes through to XCTest.
10///
11/// If XCTest is not available, you must assign your own assertion handler
12/// before using any matchers, otherwise Nimble will abort the program.
13///
14/// @see AssertionHandler
15public var NimbleAssertionHandler: AssertionHandler = { () -> AssertionHandler in
16    return isXCTestAvailable() ? NimbleXCTestHandler() : NimbleXCTestUnavailableHandler()
17}()
18
Full Screen

NimbleXCTestHandler.swift

Source: NimbleXCTestHandler.swift Github

copy
1import Foundation
2import XCTest
3
4/// Default handler for Nimble. This assertion handler passes failures along to
5/// XCTest.
6public class NimbleXCTestHandler : AssertionHandler {
7    public func assert(assertion: Bool, message: FailureMessage, location: SourceLocation) {
8        if !assertion {
9            XCTFail("\(message.stringValue)\n", file: location.file, line: location.line)
10        }
11    }
12}
13
14/// Alternative handler for Nimble. This assertion handler passes failures along
15/// to XCTest by attempting to reduce the failure message size.
16public class NimbleShortXCTestHandler: AssertionHandler {
17    public func assert(assertion: Bool, message: FailureMessage, location: SourceLocation) {
18        if !assertion {
19            let msg: String
20            if let actual = message.actualValue {
21                msg = "got: \(actual) \(message.postfixActual)"
22            } else {
23                msg = "expected \(message.to) \(message.postfixMessage)"
24            }
25            XCTFail("\(msg)\n", file: location.file, line: location.line)
26        }
27    }
28}
29
30/// Fallback handler in case XCTest is unavailable. This assertion handler will abort
31/// the program if it is invoked.
32class NimbleXCTestUnavailableHandler : AssertionHandler {
33    func assert(assertion: Bool, message: FailureMessage, location: SourceLocation) {
34        fatalError("XCTest is not available and no custom assertion handler was configured. Aborting.")
35    }
36}
37
38func isXCTestAvailable() -> Bool {
39    return NSClassFromString("XCTestCase") != nil
40}
41
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 NimbleXCTestUnavailableHandler

Trigger assert code on LambdaTest Cloud Grid

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