Quick nimble variable is always nil

2.1k views Asked by At

I create a basic test case, I cannot instantiate the variable. And while test is running, it always crashes.

import Foundation
import XCTest
import Quick
import Nimble
import UIKit

@testable import tar22102

class ViewControllerTests: QuickSpec {
   override func spec() {
    var vc: ViewController?
      beforeEach{               
          vc = ViewController()
          let _ = vc?.view
      }

      describe("testing Initial values") {
        context("Reading values") {
            expect(vc).toNot(beNil())
        }
    }
}

This is the crash log. I put breakpoint to the lines in the beforeEach, it would not reach. I also have switched to several different projects. The same issue happens.

2020-07-18 07:49:36.935064-0400 tar22102[15304:300447] Launching with XCTest injected. Preparing to run tests.
2020-07-18 07:49:37.263912-0400 tar22102[15304:300447] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An exception occurred when building Quick's example groups.
Some possible reasons this might happen include:

- An 'expect(...).to' expectation was evaluated outside of an 'it', 'context', or 'describe' block
- 'sharedExamples' was called twice with the same name
- 'itBehavesLike' was called with a name that is not registered as a shared example

Here's the original exception: 'NSInternalInconsistencyException', reason: 'Attempted to report a test failure to XCTest while no test case was running. The failure was:
"expected to not be nil, got <nil>
"
It occurred at: /Users/hzhan25/Desktop/DD/22102/22102Tests/_2102Tests.swift:27', userInfo: '(null)''
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010eb8327e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x000000010ce4db20 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010eb830bc +[NSException raise:format:] + 188
    3   22102Tests                          0x000000012b7dc8f9 __23+[QuickSpec initialize]_block_invoke + 457
    4   22102Tests                          0x000000012b7f8cda $sIyB_Ieg_TR + 10
    5   22102Tests                          0x000000012b7f8b82 $s5Quick5WorldC30performWithCurrentExampleGroup_7closureyAA0fG0C_yyXEtF + 370
    6   22102Tests                          0x000000012b7f8c9b $s5Quick5WorldC30performWithCurrentExampleGroup_7closureyAA0fG0C_yyXEtFTo + 107
    7   22102Tests                          0x000000012b7dc705 +[QuickSpec initialize] + 229
    8   libobjc.A.dylib                     0x000000010ce4e103 CALLING_SOME_+initialize_METHOD + 17
    9   libobjc.A.dylib                     0x000000010ce4eee9 initializeNonMetaClass + 624
    10  libobjc.A.dylib                     0x000000010ce4f4ba _ZL24initializeAndMaybeRelockP10objc_classP11objc_objectR8mutex_ttILb0EEb + 157
    11  libobjc.A.dylib                     0x000000010ce59a5d lookUpImpOrForward + 595
    12  libobjc.A.dylib                     0x000000010ce4a219 _objc_msgSend_uncached + 73
    13  Foundation                          0x000000010d12852b -[NSBundle loadAndReturnError:] + 1141
    14  libXCTestBundleInject.dylib         0x000000010cc46719 __XCTestBundleInject + 538
    15  ???                                 0x000000010cbb53a7 0x0 + 4508570535
    16  ???                                 0x000000010cbb57b8 0x0 + 4508571576
    17  ???                                 0x000000010cbb09a2 0x0 + 4508551586
    18  ???                                 0x000000010cbaf7a6 0x0 + 4508546982
    19  ???                                 0x000000010cbaf846 0x0 + 4508547142
    20  ???                                 0x000000010cba4046 0x0 + 4508500038
    21  ???                                 0x000000010cba80fc 0x0 + 4508516604
    22  ???                                 0x000000010cba31cd 0x0 + 4508496333
    23  ???                                 0x000000010d055234 0x0 + 4513419828
    24  ???                                 0x000000010d0530ce 0x0 + 4513411278
    25  ???                                 0x000000010d04e503 0x0 + 4513391875
    26  ???                                 0x000000010d04e036 0x0 + 4513390646
)
libc++abi.dylib: terminating with uncaught exception of type NSException
1

There are 1 answers

0
aiwiguna On BEST ANSWER

Matcher calls need to be inside an it block

describe("Initial values") {
    it("Sets vc") {
        expect(vc).toNot(beNil())
    }
}