使用realm.io进行iOS测试不起作用

Mar*_*cke 5 testing realm ios

我有一个像这样的简单领域对象:

@interface Person : RLMObject
@property NSString *name;
@end

RLM_ARRAY_TYPE(Person)
Run Code Online (Sandbox Code Playgroud)
  • 我已经为我的测试项目启用了"目标成员资格"

现在我喜欢用这种方式测试一下realm.io:

#import <XCTest/XCTest.h>
#import "Person.h"

@interface PersonTests : XCTestCase
@end

@implementation PersonTests

- (void)setUp {[super setUp];}
- (void)tearDown {[super tearDown];}
- (void)testFooBar
{
    // !!! the test crashes right here!!!!
    Person *person = [[Person alloc] init];


    person.name = @"foobar";

    RLMRealm *realm = [RLMRealm defaultRealm];

    [realm beginWriteTransaction];
    [realm addObject:person];
    [realm commitWriteTransaction];

    ......
}
Run Code Online (Sandbox Code Playgroud)

...但测试在第一行崩溃(Person*person = [[Person alloc] init];),出现以下错误

***由于未捕获的异常'RLMException'而终止应用程序,原因:'objectClass必须从RLMObject派生'

有谁知道我做错了什么?我很感谢任何暗示!!

bug*_*ibu 2

我遇到了同样的错误,经过 4 小时的删除、克隆、清理、重新安装 Pod、重复...对我有用的是:

Podfile

link_with 'MyProject', 'MyProjectTests'

#common pods such as CocoaLumberjack

pod 'Realm', '0.89.0'

target 'MyProjectTests', exclusive: true do
  pod 'Realm/Headers'
end
Run Code Online (Sandbox Code Playgroud)

测试文件

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import <Realm/Realm.h>
#import "RealmObjectSubclass.h"

- (void)setUp {
  [super setUp];
  NSString *resourcePath = [NSBundle bundleForClass:[self.class]].resourcePath;
  NSString *testRealPath = [NSString stringWithFormat:@"%@.test", resourcePath];
  [RLMRealm setDefaultRealmPath:testRealPath];
}
Run Code Online (Sandbox Code Playgroud)