-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTestExtensions.cs
More file actions
65 lines (58 loc) · 2.32 KB
/
Copy pathTestExtensions.cs
File metadata and controls
65 lines (58 loc) · 2.32 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
using System.Collections.Generic;
using System.Linq;
namespace NEventStore.Domain.Tests
{
/// <summary>
/// Extension methods especially useful for testing
/// </summary>
public static class TestExtensions
{
public static IEnumerable<TEvent> RaisedEvents<TEvent>(this IAggregate aggregate)
where TEvent : IDomainEvent
{
return aggregate.GetUncommittedEvents().OfType<TEvent>();
}
public static TEvent LastRaisedEvent<TEvent>(this IAggregate aggregate)
where TEvent : IDomainEvent
{
return aggregate.GetUncommittedEvents().OfType<TEvent>().LastOrDefault();
}
public static bool HasRaisedEvent<TEvent>(this IAggregate aggregate)
where TEvent : IDomainEvent
{
return aggregate.GetUncommittedEvents().OfType<TEvent>().Any();
}
public static IEnumerable<IDomainEvent> GetEventStream(this IStoreEvents eventStore, string streamId)
{
var stream = eventStore.OpenStream(streamId);
return stream.CommittedEvents.Select(evt => (IDomainEvent)evt.Body);
}
public static IEnumerable<IDomainEvent> GetEventStream(this IStoreEvents eventStore, string bucketId, string streamId)
{
var stream = eventStore.OpenStream(bucketId, streamId, int.MinValue, int.MaxValue);
return stream.CommittedEvents.Select(evt => (IDomainEvent)evt.Body);
}
/// <summary>
/// recupera l'ultimo evento del tipo indicato
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="eventStream"></param>
/// <returns></returns>
public static TEvent LastOfType<TEvent>(this IEnumerable<IDomainEvent> eventStream)
where TEvent : IDomainEvent
{
return eventStream.OfType<TEvent>().LastOrDefault();
}
/// <summary>
/// verifica se un evento del tipo indicato è stato lanciato
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="eventStream"></param>
/// <returns></returns>
public static bool HasAnyOfType<TEvent>(this IEnumerable<IDomainEvent> eventStream)
where TEvent : IDomainEvent
{
return eventStream.OfType<TEvent>().Any();
}
}
}