快速灵活的变量始终为零

Posted

技术标签:

【中文标题】快速灵活的变量始终为零【英文标题】:Quick nimble variable is always nil 【发布时间】:2020-07-18 12:44:23 【问题描述】:

我创建了一个基本测试用例,我无法实例化变量。而且在测试运行时,它总是崩溃。

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())
        
    

这是崩溃日志。我在 beforeEach 中的行上设置了断点,它不会到达。我也切换到了几个不同的项目。发生同样的问题。

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】:

我认为它需要在“它”里面

describe("testing Initial values") 
    context("Reading values") 
        it("Should not nil") 
            expect(vc).toNot(beNil())
        
    

【讨论】:

以上是关于快速灵活的变量始终为零的主要内容,如果未能解决你的问题,请参考以下文章

文件中的 UIImage 始终为零

使用 ObjectMapper 和 Alamofire 时对象始终为零

哪种代码更易读/快速,以确保输入中的二进制数字始终为零

类级变量集总是为零,为啥?

局部变量设置为零? (目标-C)

awakeFromNib 中的变量为零