Skip to content

Commit 9ce477f

Browse files
authored
FirebaseMessaging: Mock FIRHeartbeatInfo in test (#7995)
1 parent ea7b1e1 commit 9ce477f

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

FirebaseCore/Sources/Private/FIRHeartbeatInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ typedef NS_ENUM(NSInteger, FIRHeartbeatInfoCode) {
2727
};
2828

2929
/**
30-
* Get heartbeat code requred for the sdk.
30+
* Get heartbeat code required for the sdk.
3131
* @param heartbeatTag String representing the sdk heartbeat tag.
3232
* @return Heartbeat code indicating whether or not an sdk/global heartbeat
3333
* needs to be sent

FirebaseMessaging/Tests/UnitTests/FIRMessagingTokenOperationsTest.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#import "OCMock.h"
2020

21-
#import <GoogleUtilities/GULHeartbeatDateStorage.h>
2221
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
2322
#import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
2423
#import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
@@ -78,6 +77,7 @@ @interface FIRMessagingTokenOperationsTest : XCTestCase
7877
@property(strong, readonly, nonatomic) FIRMessagingCheckinService *checkinService;
7978
@property(strong, readonly, nonatomic) id mockCheckinService;
8079
@property(strong, readonly, nonatomic) id mockInstallations;
80+
@property(strong, readonly, nonatomic) id mockHeartbeatInfo;
8181

8282
@property(strong, readonly, nonatomic) NSString *instanceID;
8383

@@ -107,10 +107,9 @@ - (void)setUp {
107107
// Stub `FIRInstallations` to avoid using a real object.
108108
[self stubInstallations];
109109

110-
NSString *const kHeartbeatStorageFile = @"HEARTBEAT_INFO_STORAGE";
111-
GULHeartbeatDateStorage *dataStorage =
112-
[[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageFile];
113-
[[NSFileManager defaultManager] removeItemAtURL:[dataStorage fileURL] error:nil];
110+
// `FIRMessagingTokenFetchOperation` uses `FIRHeartbeatInfo` to retrieve a heartbeat code.
111+
// Stub `FIRHeartbeatInfo` to avoid using a real object.
112+
[self stubHeartbeatInfo];
114113
}
115114

116115
- (void)tearDown {
@@ -120,6 +119,7 @@ - (void)tearDown {
120119
_checkinService = nil;
121120
_mockTokenStore = nil;
122121
[_mockInstallations stopMocking];
122+
[_mockHeartbeatInfo stopMocking];
123123
}
124124

125125
- (void)testThatTokenOperationsAuthHeaderStringMatchesCheckin {
@@ -390,7 +390,9 @@ - (void)testTokenFetchOperationFirebaseUserAgentAndHeartbeatHeader {
390390
XCTAssertEqualObjects(userAgentValue, [FIRApp firebaseUserAgent]);
391391
NSString *heartBeatCode =
392392
sentRequest.allHTTPHeaderFields[kFIRMessagingFirebaseHeartbeatKey];
393-
XCTAssertEqualObjects(heartBeatCode, @"3");
393+
// It is expected that both the SDK and global heartbeat are requested.
394+
XCTAssertEqual(heartBeatCode.integerValue, FIRHeartbeatInfoCodeCombined,
395+
@"Heartbeat storage info needed to be updated but was not.");
394396
[completionExpectation fulfill];
395397

396398
return YES;
@@ -438,4 +440,10 @@ - (void)stubInstallations {
438440
OCMStub([_mockInstallations authTokenWithCompletion:authTokenWithCompletionArg]);
439441
}
440442

443+
- (void)stubHeartbeatInfo {
444+
_mockHeartbeatInfo = OCMClassMock([FIRHeartbeatInfo class]);
445+
OCMStub([_mockHeartbeatInfo heartbeatCodeForTag:@"fire-iid"])
446+
.andReturn(FIRHeartbeatInfoCodeCombined);
447+
}
448+
441449
@end

0 commit comments

Comments
 (0)