Realm在空单元测试时抛出异常

时间:2016-02-02 13:28:37

标签: objective-c swift unit-testing realm

在Objective-C项目中,我们开始在Swift中编写新的单元测试。我刚刚尝试创建第一个单元测试,成功保存解析后的JSON的结果。但是,由于以下错误,测试在setup()期间已经失败:

[ProjectTests.Project testInitializingOverlayCollectionCreatesAppropriateRealmObjects] : failed: caught "NSInvalidArgumentException", "+[RLMObjectBase ignoredProperties]: unrecognized selector sent to class 0x759b70

显然它试图在ignoredProperties类上执行RLMObjectBase,并且该方法尚未实现。不确定这是怎么发生的,因为我还没有初始化任何东西,除了创建一个带有随机内存标识符的RLMRealms对象。

ProjectTests.swift

import XCTest

class ProjectOverlayCollectionTests: XCTestCase {

    var realm: RLMRealm!

    override func setUp() {
        super.setUp()

        // Put setup code here. This method is called before the invocation of each test method in the class.
        let realmConfig = RLMRealmConfiguration()
        realmConfig.inMemoryIdentifier = NSUUID().UUIDString
        do {
            realm = try RLMRealm(configuration: realmConfig) // <-- Crashes here.
        }
        catch _ as NSError {
            XCTFail()
        }
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testInitializingOverlayCollectionCreatesAppropriateRealmObjects() {
        XCTAssertTrue(true)
    }

}

项目-桥接-Header.h

#import <Realm/Realm.h>

Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.1'

def shared_pods
    pod 'Realm', '0.95.0'
end

target 'Project' do
    shared_pods
end    

target 'ProjectTests' do
    shared_pods
end

1 个答案:

答案 0 :(得分:2)

Realm documentation中所述;

  

避免在测试目标中链接领域和测试代码

ProjectTests目标移除Realm广告连播,所有这些都与世界一致。