Unit testing 单元测试核心数据和NSExpressionDescription方法
如何对以下代码进行单元测试Unit testing 单元测试核心数据和NSExpressionDescription方法,unit-testing,core-data,Unit Testing,Core Data,如何对以下代码进行单元测试 NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; NSString *sumOfTotalPaidBySelf = @"sumOfTotalPaidBySelf"; expressionDescription.name = sumOfTotalPaidBySelf; expressionDescription.expression = [NS
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
NSString *sumOfTotalPaidBySelf = @"sumOfTotalPaidBySelf";
expressionDescription.name = sumOfTotalPaidBySelf;
expressionDescription.expression = [NSExpression expressionForKeyPath:@"@sum.moneyInMainCurrency"];
expressionDescription.expressionResultType = NSDoubleAttributeType;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MCPayment"];
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"payingPerson = %@", self];
fetchRequest.propertiesToFetch = @[expressionDescription];
NSError *error = nil;
NSArray *fetchResult = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"%@: error fetching: %@", self, error);
return nil;
}
return [[fetchResult firstObject] objectForKey:sumOfTotalPaidBySelf];
到目前为止,我学到的是,当查询已包含数据的数据库时,该代码应该可以工作,该数据存储在磁盘上。但是,对于单元测试,我使用了NSInMemoryStoreType的persistentStoreType,它在安装过程中预先填充了一些数据,这些数据仍然驻留在managedObjectContext中
这是我的单元测试
MCSharedBill *tonightsBill = [MCSharedBill addSharedBillToContext:_context];
MCPerson *fred = [tonightsBill addPerson];
fred.firstName = @"Fred";
MCPerson *anna = [tonightsBill addPerson];
anna.firstName = @"Anna";
MCPayment *drinks = [tonightsBill addPayment];
drinks.payingPerson = fred;
drinks.money = @(10);
drinks.descriptionOfPayment = @"coffee";
NSNumber *totalPaidByFred = fred.totalSumPaid;
// NSError *error = nil;
// [_context save:&error];
// XCTAssertFalse(error);
XCTAssertEqualWithAccuracy(@(10).doubleValue, totalPaidByFred.doubleValue, 0.001);
任何帮助都将不胜感激
干杯