Skip to content

Instantly share code, notes, and snippets.

@benvium
Created May 21, 2015 16:34
Show Gist options
  • Select an option

  • Save benvium/d3e377b368689684c1e0 to your computer and use it in GitHub Desktop.

Select an option

Save benvium/d3e377b368689684c1e0 to your computer and use it in GitHub Desktop.
Inherit from this to automatically create and delete [RLMRealm defaultRealm] files between tests
#import <XCTest/XCTest.h>
// Inherit from this to automatically create and delete [RLMRealm defaultRealm] files between tests
@interface RealmTestCase : XCTestCase
@end
#import "RealmTestCase.h"
#import "RLMRealm.h"
@interface RealmTestCase ()
@property(nonatomic, copy) NSString *testRealmPath;
@end
@implementation RealmTestCase
- (void)RLMDeleteRealmFilesAtPath:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:path error:nil];
NSString *lockPath = [path stringByAppendingString:@".lock"];
[fileManager removeItemAtPath:lockPath error:nil];
}
- (void)setUp {
[super setUp];
self.testRealmPath = [NSString stringWithFormat:@"%@/Library/Caches/test.realm", NSHomeDirectory()];
[self RLMDeleteRealmFilesAtPath:self.testRealmPath];
// Optionally override the default realm path.
[RLMRealm setDefaultRealmPath:self.testRealmPath];
}
- (void)tearDown {
[super tearDown];
[self RLMDeleteRealmFilesAtPath:self.testRealmPath];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment