Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS-tests with realm.io doesn't work

Tags:

ios

testing

realm

I have a simple realm object like this:

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

RLM_ARRAY_TYPE(Person)
  • I already enabled the "Target Membership" for my test project

Now I like to test something with realm.io this way:

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

    ......
}

... but the test crashes in the first line (Person *person = [[Person alloc] init];) with the following error

*** Terminating app due to uncaught exception 'RLMException', reason: 'objectClass must derive from RLMObject'

Does anyone know what I am doing wrong? I'm thankful for any hint!!

like image 215
Marco Francke Avatar asked Nov 01 '22 16:11

Marco Francke


1 Answers

I was having the same error, and after 4 hours of delete, clone, clean, re-install pods, repeat... what works for me is:

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];
}
like image 147
buguibu Avatar answered Nov 08 '22 07:11

buguibu