Skip to content

Commit d9520c1

Browse files
committed
improve unit tests
1 parent 6459249 commit d9520c1

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAppTest.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void CreateAppOptions()
104104
Assert.Same(credential, copy.Credential);
105105
Assert.Equal("test-project", copy.ProjectId);
106106
Assert.Equal("[email protected]", copy.ServiceAccountId);
107-
Assert.Equal(typeof(MockHttpClientFactory), copy.HttpClientFactory.GetType());
107+
Assert.Same(typeof(MockHttpClientFactory), copy.HttpClientFactory.GetType());
108108
}
109109

110110
[Fact]
@@ -125,7 +125,21 @@ public void CreateAppOptionsNoClientFactory()
125125
Assert.Same(credential, copy.Credential);
126126
Assert.Equal("test-project", copy.ProjectId);
127127
Assert.Equal("[email protected]", copy.ServiceAccountId);
128-
Assert.Equal(typeof(HttpClientFactory), copy.HttpClientFactory.GetType());
128+
Assert.Same(typeof(HttpClientFactory), copy.HttpClientFactory.GetType());
129+
}
130+
131+
[Fact]
132+
public void NoHttpClientFactory()
133+
{
134+
var credential = GoogleCredential.FromAccessToken("token");
135+
var options = new AppOptions()
136+
{
137+
Credential = credential,
138+
ProjectId = "test-project",
139+
ServiceAccountId = "[email protected]",
140+
HttpClientFactory = null,
141+
};
142+
Assert.Throws<ArgumentNullException>(() => FirebaseApp.Create(options));
129143
}
130144

131145
[Fact]

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/FirebaseMessagingTest.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Threading;
1717
using System.Threading.Tasks;
18+
using FirebaseAdmin.Tests;
1819
using Google.Apis.Auth.OAuth2;
1920
using Google.Apis.Http;
2021
using Xunit;
@@ -55,14 +56,25 @@ public void GetMessaging()
5556
}
5657

5758
[Fact]
58-
public void GetMessagingWithClientFactory()
59+
public async Task GetMessagingWithClientFactory()
5960
{
60-
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential, HttpClientFactory = new HttpClientFactory() }, "MyApp");
61+
var handler = new MockMessageHandler()
62+
{
63+
Response = new FirebaseMessagingClient.SendResponse()
64+
{
65+
Name = "test-response",
66+
},
67+
};
68+
var factory = new MockHttpClientFactory(handler);
69+
70+
var app = FirebaseApp.Create(new AppOptions() { Credential = GoogleCredential.FromAccessToken("test-token"), HttpClientFactory = factory, ProjectId = "test-project" });
6171
FirebaseMessaging messaging = FirebaseMessaging.GetMessaging(app);
6272
Assert.NotNull(messaging);
6373
Assert.Same(messaging, FirebaseMessaging.GetMessaging(app));
74+
75+
var response = await messaging.SendAsync(new Message() { Topic = "test-topic" });
76+
Assert.Equal("test-response", response);
6477
app.Delete();
65-
Assert.Throws<InvalidOperationException>(() => FirebaseMessaging.GetMessaging(app));
6678
}
6779

6880
[Fact]
@@ -78,8 +90,7 @@ await Assert.ThrowsAsync<ObjectDisposedException>(
7890
[Fact]
7991
public async Task SendMessageCancel()
8092
{
81-
var cred = GoogleCredential.FromFile("./resources/service_account.json");
82-
FirebaseApp.Create(new AppOptions() { Credential = cred });
93+
FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
8394
var canceller = new CancellationTokenSource();
8495
canceller.Cancel();
8596
await Assert.ThrowsAsync<OperationCanceledException>(
@@ -90,8 +101,7 @@ await Assert.ThrowsAsync<OperationCanceledException>(
90101
[Fact]
91102
public async Task SendMessageCancelWithClientFactory()
92103
{
93-
var cred = GoogleCredential.FromFile("./resources/service_account.json");
94-
FirebaseApp.Create(new AppOptions() { Credential = cred, HttpClientFactory = new HttpClientFactory() });
104+
FirebaseApp.Create(new AppOptions() { Credential = MockCredential, HttpClientFactory = new HttpClientFactory() });
95105
var canceller = new CancellationTokenSource();
96106
canceller.Cancel();
97107
await Assert.ThrowsAsync<OperationCanceledException>(

0 commit comments

Comments
 (0)