Repository: realm/realm-swift
File: Realm/Tests/RLMTestCase.m (Objective-C)

Code before:
NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end


Review comment (suggestion):
Since some apis only work on the defaultRealm (allObject for instance) so
we will need to use the default realm for testing those methods.

On Thu, May 15, 2014 at 2:28 AM, mekjaer notifications@github.com wrote:

> In Realm/Tests/RLMTestCase.m:
> 
> > @@ -38,6 +38,9 @@ @implementation RLMTestCase
> > - (void)setUp {
> >    // This method is run before every test method
> >    [super setUp];
> >   -    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
> 
> It seems like both [RLMRealm defaultRealm] and [self realmWithTestPath]
> are used in the test cases.
> @alazier https://github.com/alazier which one do you prefer, that we
> use?
> I think we should streamline and settle on one of those two.
> 
> —
> Reply to this email directly or view it on GitHubhttps://github.com/Tightdb/tightdb_objc/pull/294/files#r12680650
> .


Code after:
    return [documentsDirectory stringByAppendingPathComponent:fileName];
#else
    return fileName;
#endif
}

NSString *RLMDefaultRealmPath() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"default.realm"];
}

NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end