2016-02-27 33 views
1

我們一直在使用RLMClearRealmCache以清除測試遷移測試之間的境界的狀態。如果緩存不被清除,接下來的測試將不會執行遷移,因爲緩存仍然報告的模式是最新的,即使我們刪除並替換境界夾具文件(其中有一個老的架構)。如何在測試之間重置Realm的狀態?

RLMClearRealmCache被轉移到一個Objective-C++文件近日,所以我們要停止使用,並避免在我們的項目中使用Objective-C++。這仍然是最好的/唯一的方式嗎?

需要明確的是,我們沒有使用內存及的境界這些規範。我們有我們從一個設備在特定版本中保存的default.realm fixture文件,我們正在做以下使用:

- (void)loadBundledRealmWithName:(NSString *)name; 
{ 
    [self deleteOnDiskRealm]; 

    // copy over the file to the location 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *source = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:@"realm"]; 
    if (documentsDirectory && source) { 
     NSString *destination = [documentsDirectory stringByAppendingPathComponent:kDefaultRealmFileName]; 
     [[NSFileManager defaultManager] copyItemAtPath:source toPath:destination error:nil]; 
    } 
} 

然而,測試用例之間,但無RLMClearRealmCache一個電話,似乎雖然境界的緩存確定遷移已經運行,儘管我們已經換了.realm文件,他們需要再次運行。

回答

0

我們最終通過利用事實來清除它的緩存,如果它不再被引用,它將這樣做。這只是一個有點棘手追查這樣做停止它的問題:我們保留測試運行之間的領域對象的引用:

context(@"some context", ^{ 
    __block MyRealmObject *whoops; 

    beforeEach(^{ 
     [specHelper loadBundledRealmWithName:@"release-50-fixture.realm"]; 
     [migrationManager performMigrations]; 
     whoops = [[MyRealmObject allObjects] firstObject]; 
    }); 

    it(@"first", ^{ 
     // migrations will run for this `it` 
    }); 

    it(@"second", ^{ 
     // migrations will NOT run for this `it` since the old Realm is still around and its cache thinks migrations have already run (even though we've swapped out the backing DB). 
     // the issue is that `whoops` is retaining a `MyRealmObject` and thus the Realm. 
    }); 
}); 
2

您可以使用單獨的內存領域的每個測試。當你這樣做時,每個測試都會得到一個「新鮮」的領域,而領域的狀態不會從一個測試泄漏到另一個測試。

爲了實現這一切,您需要在運行前將Realm的配置inMemoryIdentifer設置爲當前測試的名稱。你可以在你的XCTestCasesetUp方法(如域文檔建議):

override func setUp() { 
    super.setUp() 
    Realm.Configuration.defaultConfiguration.inMemoryIdentifier = self.name 
} 

編輯:

這個答案不適合更新的問題,但我會在這裏反正離開它因爲它可以幫助其他人尋找重置測試之間Realm狀態的方法。

+0

我應該更清楚,我們沒有使用IN-內存領域。我已經更新了我的問題。 – solidcell

+0

啊,好的。我是否應該刪除這個答案,或者我們是否應該保留這個答案,以防其他人搜索「重置測試之間的區域狀態」? – joern

+0

是的,我認爲離開它只能幫助別人。 – solidcell

相關問題