-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEventExpectations.cs
More file actions
88 lines (76 loc) · 3.29 KB
/
Copy pathEventExpectations.cs
File metadata and controls
88 lines (76 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
namespace CoreEx.UnitTesting.Events;
/// <summary>
/// Provides <see cref="IEventPublisher"/>-specific expectations for unit testing.
/// </summary>
/// <typeparam name="TTester">The tester <see cref="Type"/>.</typeparam>
public class EventExpectations<TTester> : ExpectationsBase<TTester>
{
private readonly Dictionary<string, EventExpectationsConfig> _expectations = [];
/// <summary>
/// Initializes a new instance of the <see cref="EventExpectations{TTester}"/> class.
/// </summary>
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
/// <param name="tester">The initiating tester.</param>
/// <param name="requestId">The request identifier.</param>
/// <param name="assembly">The assembly to use for resource resolution.</param>
internal EventExpectations(TesterBase owner, TTester tester, string? requestId, Assembly assembly) : base(owner, tester)
{
RequestId = requestId;
ResourceAssembly = assembly;
}
/// <inheritdoc/>
public override string Title => "Event expectations";
/// <inheritdoc/>
/// <remarks>Overrides to ensure it occurs after majority.</remarks>
public override int Order => 5000;
/// <summary>
/// Gets the request identifier.
/// </summary>
internal string? RequestId { get; }
/// <summary>
/// Gets the assembly to use for resource resolution.
/// </summary>
internal Assembly ResourceAssembly { get; }
/// <summary>
/// Expects that no events will have been published for the keyed <see cref="IEventPublisher"/>.
/// </summary>
/// <param name="serviceKey">The service key used for the keyed registration.</param>
/// <remarks>The <paramref name="serviceKey"/> must be the same as used when registering the underlying <see cref="IEventPublisher"/>.</remarks>
public void ExpectNoEvents(string serviceKey)
{
GetOrAddConfig(serviceKey).ExpectNoEvents();
}
/// <summary>
/// Expects that events will have been published for the keyed <see cref="IEventPublisher"/>.
/// </summary>
/// <param name="serviceKey">The service key used for the keyed registration.</param>
/// <param name="configure">The action to enable events expectations configuration.</param>
/// <remarks>The <paramref name="serviceKey"/> must be the same as used when registering the underlying <see cref="IEventPublisher"/>.</remarks>
public void ExpectEvents(string serviceKey, Action<EventExpectationsConfig>? configure = null)
{
var config = GetOrAddConfig(serviceKey);
config.ExpectEvents();
configure?.Invoke(config);
}
/// <summary>
/// Gets or adds the expectation configuration for the specified service key.
/// </summary>
private EventExpectationsConfig GetOrAddConfig(string serviceKey)
{
if (!_expectations.TryGetValue(serviceKey.ThrowIfNullOrEmpty(), out var config))
{
config = new EventExpectationsConfig(Owner, serviceKey, RequestId, ResourceAssembly);
_expectations[serviceKey] = config;
}
return config;
}
/// <inheritdoc/>
protected override Task OnAssertAsync(AssertArgs args)
{
foreach (var kvp in _expectations)
{
kvp.Value.Assert(args);
}
return Task.CompletedTask;
}
}