Repository navigation
Expand file tree
/
Copy pathEventAggregationAutoSubscriptionModuleTests.cs
More file actions
63 lines (47 loc) · 2.39 KB
/
Copy pathEventAggregationAutoSubscriptionModuleTests.cs
File metadata and controls
63 lines (47 loc) · 2.39 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
namespace ServiceControl.Config.Tests
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Caliburn.Micro;
using Framework.Modules;
using NUnit.Framework;
[TestFixture]
public class EventAggregationAutoSubscriptionModuleTests
{
[Test]
public void AutoSubscribesOnResolve()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new EventAggregationAutoSubscriptionModule());
builder.RegisterType<FakeEventAggregator>().As<IEventAggregator>().AsSelf().SingleInstance();
builder.RegisterType<FakeEventHandler>();
builder.RegisterType<FakeNonEventHandler>();
var container = builder.Build();
var events = container.Resolve<FakeEventAggregator>();
Assert.That(events.Subscribers, Is.Empty, "There should be no handlers until they are resolved");
var handler = container.Resolve<FakeEventHandler>();
Assert.That(events.Subscribers, Does.Contain(handler), "Items should be subscribed on activation");
var nonHandler = container.Resolve<FakeNonEventHandler>();
Assert.That(events.Subscribers, Does.Not.Contain(nonHandler), "Non-handlers should not be subscribed on activation");
}
class FakeEvent;
class FakeEventHandler : IHandle<FakeEvent>
{
public Task HandleAsync(FakeEvent message, CancellationToken cancellationToken = default) => throw new NotImplementedException();
}
class FakeNonEventHandler;
class FakeEventAggregator : IEventAggregator
{
public List<object> Subscribers = [];
public bool HandlerExistsFor(Type messageType) => throw new NotImplementedException();
#pragma warning disable PS0013 // Caliburn.Micro's IEventAggregator declares the marshal delegate as Func<Func<Task>, Task>
public void Subscribe(object subscriber, Func<Func<Task>, Task> marshal) => Subscribers.Add(subscriber);
public void Unsubscribe(object subscriber) => throw new NotImplementedException();
public Task PublishAsync(object message, Func<Func<Task>, Task> marshal, CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
#pragma warning restore PS0013
}
}
}