在TestCases中获取iPhone文档目录 [英] Getting iPhone Documents Directory in TestCases
问题描述
我正在运行测试用例,其中使用sqlite数据库使用默认的SenTestingkit在iPhone模拟器上测试我的代码, 当我尝试使用以下两种方法之一获取主目录时
I'm running test cases which uses sqlite database to test my code on iPhone simulator using default SenTestingkit, when i try to get the home directory using either of the following ways
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
或
NSString *home = NSHomeDirectory();
我走错了路,指向接下来的洗澡
i got a wrong path that points to the following bath
/Users/{user}/Library/Application Support/iPhone Simulator/5.1
,我无法创建sqlite数据库, 知道如何在测试用例中找到正确的路径吗?
and i couldn't create the sqlite database, any idea how to get the right path in the Test cases?
谢谢
推荐答案
我找到了答案:)
首先,我必须检测我是处于TestCase还是正在使用SenClass类的NSClassFromString运行项目,然后根据每种情况构造路径
first I had to detect whether i'm in TestCase or running project using NSClassFromString for the SenTest Class then construct the path according to each case
if (NSClassFromString(@"SenTest") == nil) {
// Running project -> path inside the Document folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
path = [docsPath stringByAppendingPathComponent:KDatabaseName];
} else {
// Test case -> path inside "UnitTests" folder in the project directory
NSString *directory = [[NSFileManager defaultManager] currentDirectoryPath];
path = [directory stringByAppendingPathComponent:@"UnitTests/"];
path = [path stringByAppendingPathComponent:KDatabaseName];
}
这篇关于在TestCases中获取iPhone文档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!