Skip to content

Commit b1dfcfd

Browse files
IID: fix flaky tests (#4703)
* IID: fix flaky tests * Coments.
1 parent 5d5c427 commit b1dfcfd

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Example/InstanceID/Tests/FIRInstanceIDTokenOperationsTest.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#import <XCTest/XCTest.h>
1818

1919
#import <FirebaseInstanceID/FIRInstanceID.h>
20+
2021
#import <OCMock/OCMock.h>
22+
2123
#import "Firebase/InstanceID/FIRInstanceIDAuthService.h"
2224
#import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences+Internal.h"
2325
#import "Firebase/InstanceID/FIRInstanceIDCheckinService.h"
@@ -31,6 +33,7 @@
3133
#import "Firebase/InstanceID/NSError+FIRInstanceID.h"
3234

3335
#import <FirebaseCore/FIRAppInternal.h>
36+
#import <FirebaseInstallations/FirebaseInstallations.h>
3437
#import <GoogleUtilities/GULHeartbeatDateStorage.h>
3538

3639
static NSString *kDeviceID = @"fakeDeviceID";
@@ -45,6 +48,10 @@ @interface FIRInstanceIDTokenOperation (ExposedForTest)
4548
- (void)performTokenOperation;
4649
@end
4750

51+
@interface FIRInstallationsAuthTokenResult (Tests)
52+
- (instancetype)initWithToken:(NSString *)token expirationDate:(NSDate *)expirationDate;
53+
@end
54+
4855
@interface FIRInstanceIDTokenOperationsTest : XCTestCase
4956

5057
@property(strong, readonly, nonatomic) FIRInstanceIDAuthService *authService;
@@ -53,6 +60,7 @@ @interface FIRInstanceIDTokenOperationsTest : XCTestCase
5360
@property(strong, readonly, nonatomic) id mockStore;
5461
@property(strong, readonly, nonatomic) FIRInstanceIDCheckinService *checkinService;
5562
@property(strong, readonly, nonatomic) id mockCheckinService;
63+
@property(strong, readonly, nonatomic) id mockInstallations;
5664

5765
@property(strong, readonly, nonatomic) NSString *instanceID;
5866

@@ -71,12 +79,26 @@ - (void)setUp {
7179
store:_mockStore];
7280
_instanceID = @"instanceID";
7381

82+
// `FIRInstanceIDTokenOperation` uses `FIRInstallations` under the hood to get FIS auth token.
83+
// Stub `FIRInstallations` to avoid using a real object.
84+
[self stubInstallations];
85+
7486
NSString *const kHeartbeatStorageFile = @"HEARTBEAT_INFO_STORAGE";
7587
GULHeartbeatDateStorage *dataStorage =
7688
[[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageFile];
7789
[[NSFileManager defaultManager] removeItemAtURL:[dataStorage fileURL] error:nil];
7890
}
7991

92+
- (void)tearDown {
93+
[_mockInstallations stopMocking];
94+
_mockInstallations = nil;
95+
_authService = nil;
96+
[_mockCheckinService stopMocking];
97+
_mockCheckinService = nil;
98+
_checkinService = nil;
99+
_mockStore = nil;
100+
}
101+
80102
- (void)testThatTokenOperationsAuthHeaderStringMatchesCheckin {
81103
int64_t tenHoursAgo = FIRInstanceIDCurrentTimestampInMilliseconds() - 10 * 60 * 60 * 1000;
82104
FIRInstanceIDCheckinPreferences *checkin =
@@ -371,4 +393,14 @@ - (FIRInstanceIDCheckinPreferences *)setCheckinPreferencesWithLastCheckinTime:(i
371393
return checkinPreferences;
372394
}
373395

396+
- (void)stubInstallations {
397+
_mockInstallations = OCMClassMock([FIRInstallations class]);
398+
OCMStub([_mockInstallations installations]).andReturn(_mockInstallations);
399+
FIRInstallationsAuthTokenResult *authToken =
400+
[[FIRInstallationsAuthTokenResult alloc] initWithToken:@"fis-auth-token"
401+
expirationDate:[NSDate distantFuture]];
402+
id authTokenWithCompletionArg = [OCMArg invokeBlockWithArgs:authToken, [NSNull null], nil];
403+
OCMStub([_mockInstallations authTokenWithCompletion:authTokenWithCompletionArg]);
404+
}
405+
374406
@end

0 commit comments

Comments
 (0)