Skip to content

Commit cb4d4dc

Browse files
committed
test: 增加单元测试
1 parent 28bf162 commit cb4d4dc

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Runtime.CompilerServices;
7+
8+
namespace UnitTest.Services;
9+
10+
public class BootstrapBlazorRootRegisterServiceTest
11+
{
12+
[Fact]
13+
public void Provider_Ok()
14+
{
15+
var service = new BootstrapBlazorRootRegisterService();
16+
var exception = Assert.ThrowsAny<InvalidOperationException>(() => service.RemoveProvider(new object(), new BootstrapBlazorRootContent()));
17+
Assert.Equal("There are no content providers with the given root ID 'System.Object'.", exception.Message);
18+
19+
var identifier = new object();
20+
service.AddProvider(identifier, new BootstrapBlazorRootContent());
21+
exception = Assert.ThrowsAny<InvalidOperationException>(() => service.RemoveProvider(identifier, new BootstrapBlazorRootContent()));
22+
Assert.Equal("The provider was not found in the providers list of the given root ID 'System.Object'.", exception.Message);
23+
}
24+
25+
[Fact]
26+
public void Subscribe_Ok()
27+
{
28+
var service = new BootstrapBlazorRootRegisterService();
29+
var identifier = new object();
30+
service.Subscribe(identifier, new BootstrapBlazorRootOutlet());
31+
var exception = Assert.ThrowsAny<InvalidOperationException>(() => service.Subscribe(identifier, new BootstrapBlazorRootOutlet()));
32+
Assert.Equal("There is already a subscriber to the content with the given root ID 'System.Object'.", exception.Message);
33+
34+
exception = Assert.ThrowsAny<InvalidOperationException>(() => service.Unsubscribe(new object()));
35+
Assert.Equal("The subscriber with the given root ID 'System.Object' is already unsubscribed.", exception.Message);
36+
}
37+
38+
[Fact]
39+
public void NotifyContentProviderChanged_Ok()
40+
{
41+
var service = new BootstrapBlazorRootRegisterService();
42+
var exception = Assert.ThrowsAny<InvalidOperationException>(() => service.NotifyContentProviderChanged(new object(), new BootstrapBlazorRootContent()));
43+
Assert.Equal("There are no content providers with the given root ID 'System.Object'.", exception.Message);
44+
45+
46+
var identifier = new object();
47+
service.AddProvider(identifier, new BootstrapBlazorRootContent());
48+
49+
var providers = service.GetProviders(identifier);
50+
providers.Clear();
51+
service.NotifyContentProviderChanged(identifier, new BootstrapBlazorRootContent());
52+
}
53+
54+
[Fact]
55+
public void GetCurrentProviderContentOrDefault_Ok()
56+
{
57+
var service = new BootstrapBlazorRootRegisterService();
58+
Assert.Null(GetCurrentProviderContentOrDefault(service, []));
59+
60+
var provider = new BootstrapBlazorRootContent();
61+
service.AddProvider(new object(), provider);
62+
Assert.Equal(provider, GetCurrentProviderContentOrDefault(service, [provider]));
63+
}
64+
65+
66+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "GetCurrentProviderContentOrDefault")]
67+
static extern BootstrapBlazorRootContent? GetCurrentProviderContentOrDefault(BootstrapBlazorRootRegisterService @this, List<BootstrapBlazorRootContent> providers);
68+
69+
}

0 commit comments

Comments
 (0)