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

时间:2014-10-03 17:12:34

标签: ios testing realm

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

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

RLM_ARRAY_TYPE(Person)
  • 我已启用"目标会员资格"对于我的测试项目

现在我喜欢用这种方式测试一下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];

    ......
}

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

  

***因未捕获的异常终止应用程序&#39; RLMException&#39;,原因:&#39; objectClass必须来自RLMObject&#39;

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

1 个答案:

答案 0 :(得分:2)

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

Podfile

link_with 'MyProject', 'MyProjectTests'

#common pods such as CocoaLumberjack

pod 'Realm', '0.89.0'

target 'MyProjectTests', exclusive: true do
  pod 'Realm/Headers'
end

TESTFILE

#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];
}