From 7f7592ab5326177f22ded65d8e37fdaa77bef55b Mon Sep 17 00:00:00 2001 From: Hermes Pique Date: Wed, 3 Dec 2014 01:40:45 +0000 Subject: [PATCH] Improve unit tests involving observers --- RMStoreTests/RMStoreTests.m | 148 +++++++++++++----------------------- 1 file changed, 54 insertions(+), 94 deletions(-) diff --git a/RMStoreTests/RMStoreTests.m b/RMStoreTests/RMStoreTests.m index 8b523f20..d6e27956 100644 --- a/RMStoreTests/RMStoreTests.m +++ b/RMStoreTests/RMStoreTests.m @@ -27,15 +27,6 @@ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-retain-cycles" // To use ST macros in blocks -extern NSString* const RMSKRefreshReceiptFailed; -extern NSString* const RMSKRefreshReceiptFinished; -extern NSString* const RMSKRestoreTransactionsFinished; -extern NSString* const RMStoreNotificationProductIdentifier; -extern NSString* const RMStoreNotificationTransaction; -extern NSString* const RMStoreNotificationTransactions; - -extern NSString* const RMStoreNotificationStoreError; - @interface RMStoreTests : XCTestCase @end @@ -748,17 +739,14 @@ - (void)testPaymentQueueUpdatedTransactions_Purchased__VerificatorUnableToComple - (void)testPaymentQueueUpdatedTransactions_Restored__NoVerificator_NoDownloader { id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; - id transaction = [self mockPaymentTransactionWithState:SKPaymentTransactionStateRestored]; - id originalTransaction = [self mockPaymentTransactionWithState:SKPaymentTransactionStatePurchased]; - [[[transaction stub] andReturn:originalTransaction] originalTransaction]; + id transaction = [self mockRestoredPaymentTransaction]; [[queue stub] finishTransaction:[OCMArg any]]; - - id observerMock = [OCMockObject observerMock]; - [[NSNotificationCenter defaultCenter] addMockObserver:observerMock name:RMSKRestoreTransactionsFinished object:_store]; + [[_observer expect] storePaymentTransactionFinished:[OCMArg isNotNil]]; + [_store addStoreObserver:_observer]; [_store paymentQueue:queue updatedTransactions:@[transaction]]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueUpdatedTransactions_Restored__NoVerificator_DownloaderSuccess @@ -845,22 +833,22 @@ - (void)testPaymentQueueUpdatedTransactions_RestoredWithDownloads [queue verify]; } -- (void)testPaymentQueueUpdatedTransactions_Restored__ExpectRMSKRestoreTransactionsFinished +- (void)testPaymentQueueUpdatedTransactions_Restored__storeRestoreTransactionsFinished { id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; id transaction = [self mockRestoredPaymentTransaction]; [[queue stub] finishTransaction:[OCMArg any]]; [_store paymentQueueRestoreCompletedTransactionsFinished:queue]; - id observerMock = [self observerMockForNotification:RMSKRestoreTransactionsFinished checkUserInfoWithBlock:^BOOL(NSDictionary *userInfo) { - NSArray *transactions = userInfo[RMStoreNotificationTransactions]; - XCTAssertEqualObjects(transactions, @[transaction]); + [[_observer expect] storePaymentTransactionFinished:[OCMArg isNotNil]]; + [[_observer expect] storeRestoreTransactionsFinished:[OCMArg checkWithBlock:^BOOL(NSNotification *notification) { + XCTAssertEqualObjects(notification.rm_transactions, @[transaction]); return YES; - }]; + }]]; + [_store addStoreObserver:_observer]; [_store paymentQueue:queue updatedTransactions:@[transaction]]; - [observerMock verify]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueUpdatedTransactions_Restored__Twice @@ -872,14 +860,12 @@ - (void)testPaymentQueueUpdatedTransactions_Restored__Twice [_store paymentQueueRestoreCompletedTransactionsFinished:queue]; [_store paymentQueue:queue updatedTransactions:@[transaction]]; [_store restoreTransactions]; - - id observerMock = [OCMockObject observerMock]; - [[NSNotificationCenter defaultCenter] addMockObserver:observerMock name:RMSKRestoreTransactionsFinished object:_store]; + [[_observer expect] storePaymentTransactionFinished:[OCMArg isNotNil]]; + [_store addStoreObserver:_observer]; [_store paymentQueue:queue updatedTransactions:@[transaction]]; - [observerMock verify]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueUpdatedTransactions_Restored__VerificatorSuccess @@ -889,13 +875,12 @@ - (void)testPaymentQueueUpdatedTransactions_Restored__VerificatorSuccess id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; id transaction = [self mockRestoredPaymentTransaction]; [[queue stub] finishTransaction:[OCMArg any]]; - - id observerMock = [OCMockObject observerMock]; - [[NSNotificationCenter defaultCenter] addMockObserver:observerMock name:RMSKRestoreTransactionsFinished object:_store]; + [[_observer expect] storePaymentTransactionFinished:[OCMArg isNotNil]]; + [_store addStoreObserver:_observer]; [_store paymentQueue:queue updatedTransactions:@[transaction]]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueUpdatedTransactions_Restored__VerificatorFailure @@ -905,13 +890,12 @@ - (void)testPaymentQueueUpdatedTransactions_Restored__VerificatorFailure id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; id transaction = [self mockRestoredPaymentTransaction]; [[queue stub] finishTransaction:[OCMArg any]]; - - id observerMock = [OCMockObject observerMock]; - [[NSNotificationCenter defaultCenter] addMockObserver:observerMock name:RMSKRestoreTransactionsFinished object:_store]; + [[_observer expect] storePaymentTransactionFailed:[OCMArg isNotNil]]; + [_store addStoreObserver:_observer]; [_store paymentQueue:queue updatedTransactions:@[transaction]]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueUpdatedTransactions_Failed @@ -970,26 +954,25 @@ - (void)testPaymentQueueUpdatedTransactions_Deferred - (void)testPaymentQueueRestoreCompletedTransactionsFinished { - id observerMock = [self observerMockForNotification:RMSKRestoreTransactionsFinished checkUserInfoWithBlock:^BOOL(NSDictionary *userInfo) { - NSArray *transactions = userInfo[RMStoreNotificationTransactions]; - XCTAssertEqualObjects(transactions, @[], @""); + [[_observer expect] storeRestoreTransactionsFinished:[OCMArg checkWithBlock:^BOOL(NSNotification *notification) { + XCTAssertEqualObjects(notification.rm_transactions, @[]); return YES; - }]; + }]]; + [_store addStoreObserver:_observer]; id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; [_store paymentQueueRestoreCompletedTransactionsFinished:queue]; - [observerMock verify]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueRestoreCompletedTransactionsFinished_Queue__Blocks { - id observerMock = [self observerMockForNotification:RMSKRestoreTransactionsFinished checkUserInfoWithBlock:^BOOL(NSDictionary *userInfo) { - NSArray *transactions = userInfo[RMStoreNotificationTransactions]; - XCTAssertEqualObjects(transactions, @[], @""); + [[_observer expect] storeRestoreTransactionsFinished:[OCMArg checkWithBlock:^BOOL(NSNotification *notification) { + XCTAssertEqualObjects(notification.rm_transactions, @[]); return YES; - }]; + }]]; + [_store addStoreObserver:_observer]; id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; __block BOOL didSucceed = NO; [_store restoreTransactionsOnSuccess:^(NSArray* transactions) { @@ -1001,13 +984,11 @@ - (void)testPaymentQueueRestoreCompletedTransactionsFinished_Queue__Blocks [_store paymentQueueRestoreCompletedTransactionsFinished:queue]; - [observerMock verify]; + [_observer verify]; XCTAssertTrue(didSucceed, @""); - - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; } -- (void)testPaymentQueueRestoreCompletedTransactionsFinished_Queue__TwoTransactions_ExpectRMSKRestoreTransactionsFinished +- (void)testPaymentQueueRestoreCompletedTransactionsFinished_Queue__TwoTransactions_storeRestoreTransactionsFinished { id queue = [OCMockObject mockForClass:[SKPaymentQueue class]]; [[queue stub] finishTransaction:[OCMArg any]]; @@ -1015,16 +996,15 @@ - (void)testPaymentQueueRestoreCompletedTransactionsFinished_Queue__TwoTransacti id transaction2 = [self mockRestoredPaymentTransaction]; NSArray *updatedTransactions = @[transaction1, transaction2]; [_store paymentQueue:queue updatedTransactions:updatedTransactions]; - id observerMock = [self observerMockForNotification:RMSKRestoreTransactionsFinished checkUserInfoWithBlock:^BOOL(NSDictionary *userInfo) { - NSArray *transactions = userInfo[RMStoreNotificationTransactions]; - XCTAssertEqualObjects(transactions, updatedTransactions, @""); + [[_observer expect] storeRestoreTransactionsFinished:[OCMArg checkWithBlock:^BOOL(NSNotification *notification) { + XCTAssertEqualObjects(notification.rm_transactions, updatedTransactions, @""); return YES; - }]; + }]]; + [_store addStoreObserver:_observer]; [_store paymentQueueRestoreCompletedTransactionsFinished:queue]; - [observerMock verify]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testPaymentQueueRestoreCompletedTransactionsFinished_Queue__TwoTransactions_restoreTransactionsOnSuccess_SuccessBlock @@ -1126,38 +1106,38 @@ - (void)testPaymentQueueRestoreCompletedTransactionsFailedWithError_Queue_Error_ - (void)testRequestDidFinish_noBlocks { SKIP_IF_VERSION(NSFoundationVersionNumber_iOS_6_1) - id observerMock = [self observerMockForNotification:RMSKRefreshReceiptFinished]; + [[_observer expect] storeRefreshReceiptFinished:[OCMArg isNotNil]]; + [_store addStoreObserver:_observer]; id store = _store; id requestMock = [OCMockObject mockForClass:[SKRequest class]]; [store requestDidFinish:requestMock]; - [observerMock verify]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testRequestDidFailWithError_noBlocks { SKIP_IF_VERSION(NSFoundationVersionNumber_iOS_6_1) NSError *originalError = [NSError errorWithDomain:@"test" code:0 userInfo:nil]; - id observerMock = [self observerMockForNotification:RMSKRefreshReceiptFailed checkUserInfoWithBlock:^BOOL(NSDictionary *userInfo) { - NSError *error = userInfo[RMStoreNotificationStoreError]; - XCTAssertEqualObjects(error, originalError, @""); + [[_observer expect] storeRefreshReceiptFailed:[OCMArg checkWithBlock:^BOOL(NSNotification *notification) { + XCTAssertEqualObjects(notification.rm_storeError, originalError, @""); return YES; - }]; + }]]; + [_store addStoreObserver:_observer]; id store = _store; id requestMock = [OCMockObject mockForClass:[SKRequest class]]; [store request:requestMock didFailWithError:originalError]; - [observerMock verify]; - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; + [_observer verify]; } - (void)testRequestDidFinish_withBlocks { SKIP_IF_VERSION(NSFoundationVersionNumber_iOS_6_1) - __block BOOL executed; - id observerMock = [self observerMockForNotification:RMSKRefreshReceiptFinished]; + [[_observer expect] storeRefreshReceiptFinished:[OCMArg isNotNil]]; + [_store addStoreObserver:_observer]; + __block BOOL executed = NO; [_store refreshReceiptOnSuccess:^{ executed = YES; } failure:^(NSError *error) { @@ -1168,20 +1148,19 @@ - (void)testRequestDidFinish_withBlocks id requestMock = [OCMockObject mockForClass:[SKRequest class]]; [store requestDidFinish:requestMock]; - [observerMock verify]; + [_observer verify]; XCTAssertTrue(executed, @""); - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; } - (void)testRequestDidFailWithError_withBlocks { SKIP_IF_VERSION(NSFoundationVersionNumber_iOS_6_1) - __block BOOL executed; NSError *originalError = [NSError errorWithDomain:@"test" code:0 userInfo:nil]; - id observerMock = [self observerMockForNotification:RMSKRefreshReceiptFailed checkUserInfoWithBlock:^BOOL(NSDictionary *userInfo) { - NSError *error = userInfo[RMStoreNotificationStoreError]; - XCTAssertEqualObjects(error, originalError, @""); + [[_observer expect] storeRefreshReceiptFailed:[OCMArg checkWithBlock:^BOOL(NSNotification *notification) { + XCTAssertEqualObjects(notification.rm_storeError, originalError, @""); return YES; - }]; + }]]; + [_store addStoreObserver:_observer]; + __block BOOL executed = NO; [_store refreshReceiptOnSuccess:^{ XCTFail(@""); } failure:^(NSError *error) { @@ -1195,10 +1174,8 @@ - (void)testRequestDidFailWithError_withBlocks [store request:requestMock didFailWithError:originalError]; - [observerMock verify]; + [_observer verify]; XCTAssertTrue(executed, @""); - - [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; } #pragma mark Private @@ -1331,22 +1308,6 @@ - (id)mockPaymentTransactionWithState:(SKPaymentTransactionState)state downloads return transaction; } -- (id)observerMockForNotification:(NSString*)name -{ - id mock = [OCMockObject observerMock]; - [[NSNotificationCenter defaultCenter] addMockObserver:mock name:name object:_store]; - [[mock expect] notificationWithName:name object:_store]; - return mock; -} - -- (id)observerMockForNotification:(NSString*)name checkUserInfoWithBlock:(BOOL(^)(id obj))block -{ - id mock = [OCMockObject observerMock]; - [[NSNotificationCenter defaultCenter] addMockObserver:mock name:name object:_store]; - [[mock expect] notificationWithName:name object:_store userInfo:[OCMArg checkWithBlock:block]]; - return mock; -} - #pragma mark RMStoreObserver - (void)storeProductsRequestFailed:(NSNotification*)notification {} @@ -1414,4 +1375,3 @@ - (void)verifyTransaction:(SKPaymentTransaction *)transaction success:(void (^)( @end #pragma clang diagnostic pop -