How to use expectFailureMessageForNil method of func class

Best Nimble code snippet using func.expectFailureMessageForNil

Run Nimble automation tests on LambdaTest cloud grid

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

utils.swift

Source: utils.swift Github

copy
1import Foundation
2import Nimble
3import XCTest
4
5func failsWithErrorMessage(message: String, closure: () -> Void, file: String = __FILE__, line: UInt = __LINE__, preferOriginalSourceLocation: Bool = false) {
6    var filePath = file
7    var lineNumber = line
8
9    let recorder = AssertionRecorder()
10    withAssertionHandler(recorder, closure)
11
12    var lastFailure: AssertionRecord?
13    if recorder.assertions.count > 0 {
14        lastFailure = recorder.assertions[recorder.assertions.endIndex - 1]
15        if lastFailure!.message == message {
16            return
17        }
18    }
19
20    if preferOriginalSourceLocation {
21        if let failure = lastFailure {
22            filePath = failure.location.file
23            lineNumber = failure.location.line
24        }
25    }
26
27    if lastFailure != nil {
28        let msg = "Got failure message: \"\(lastFailure!.message)\", but expected \"\(message)\""
29        XCTFail(msg, file: filePath, line: lineNumber)
30    } else {
31        XCTFail("expected failure message, but got none", file: filePath, line: lineNumber)
32    }
33}
34
35func failsWithErrorMessageForNil(message: String, closure: () -> Void, file: String = __FILE__, line: UInt = __LINE__, preferOriginalSourceLocation: Bool = false) {
36    failsWithErrorMessage("\(message) (use beNil() to match nils)", closure, file: file, line: line, preferOriginalSourceLocation: preferOriginalSourceLocation)
37}
38
39func deferToMainQueue(action: () -> Void) {
40    dispatch_async(dispatch_get_main_queue()) {
41        NSThread.sleepForTimeInterval(0.01)
42        action()
43    }
44}
45
46public class NimbleHelper : NSObject {
47    class func expectFailureMessage(message: NSString, block: () -> Void, file: String, line: UInt) {
48        failsWithErrorMessage(message as String, block, file: file, line: line, preferOriginalSourceLocation: true)
49    }
50
51    class func expectFailureMessageForNil(message: NSString, block: () -> Void, file: String, line: UInt) {
52        failsWithErrorMessageForNil(message as String, block, file: file, line: line, preferOriginalSourceLocation: true)
53    }
54}
55
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
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)