-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompleteEventBenchmarks.cs
More file actions
120 lines (100 loc) · 3.33 KB
/
Copy pathCompleteEventBenchmarks.cs
File metadata and controls
120 lines (100 loc) · 3.33 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using BenchmarkDotNet.Attributes;
using FakeItEasy;
using Imposter.Abstractions;
using Mockolate.Benchmarks;
using Mockolate.Verify;
using NSubstitute;
using Arg = NSubstitute.Arg;
using Raise = NSubstitute.Raise;
using Times = Moq.Times;
[assembly: GenerateImposter(typeof(CompleteEventBenchmarks.IMyEventInterface))]
namespace Mockolate.Benchmarks;
#pragma warning disable CA1822 // Mark members as static
/// <summary>
/// In this benchmark we check the case of an interface mock with an event, subscribe to the event and verify
/// the subscription was recorded once.
/// </summary>
public class CompleteEventBenchmarks : BenchmarksBase
{
/// <summary>
/// <see href="https://docs.testably.org/Mockolate" />
/// </summary>
[Benchmark(Baseline = true)]
public void Event_Mockolate()
{
IMyEventInterface sut = IMyEventInterface.CreateMock();
EventHandler handler = (_, _) => { };
sut.SomeEvent += handler;
sut.Mock.Raise.SomeEvent(null, EventArgs.Empty);
sut.Mock.Verify.SomeEvent.Subscribed().Once();
}
/// <summary>
/// <see href="https://github.com/themidnightgospel/Imposter" />
/// </summary>
[Benchmark]
public void Event_Imposter()
{
IMyEventInterfaceImposter imposter = IMyEventInterface.Imposter();
EventHandler handler = (_, _) => { };
imposter.Instance().SomeEvent += handler;
imposter.SomeEvent.Raise(null!, EventArgs.Empty);
imposter.SomeEvent.Subscribed(handler, Count.Once());
}
/// <summary>
/// <see href="https://github.com/thomhurst/TUnit/" />
/// </summary>
[Benchmark]
public void Event_TUnitMocks()
{
Mock<IMyEventInterface> mock = TUnit.Mocks.Mock.Of<IMyEventInterface>();
EventHandler handler = (_, _) => { };
mock.Object.SomeEvent += handler;
mock.RaiseSomeEvent(EventArgs.Empty);
_ = mock.Events.SomeEvent.SubscriberCount;
}
/// <summary>
/// <see href="https://github.com/devlooped/moq" />
/// </summary>
[Benchmark]
public void Event_Moq()
{
Moq.Mock<IMyEventInterface> mock = new();
mock.SetupAdd(m => m.SomeEvent += Moq.It.IsAny<EventHandler>());
EventHandler handler = (_, _) => { };
mock.Object.SomeEvent += handler;
mock.Raise(m => m.SomeEvent += null, null!, EventArgs.Empty);
mock.VerifyAdd(m => m.SomeEvent += Moq.It.IsAny<EventHandler>(), Times.Once());
}
/// <summary>
/// <see href="https://nsubstitute.github.io/" />
/// </summary>
[Benchmark]
public void Event_NSubstitute()
{
IMyEventInterface mock = Substitute.For<IMyEventInterface>();
EventHandler handler = (_, _) => { };
mock.SomeEvent += handler;
mock.SomeEvent += Raise.EventWith(null!, EventArgs.Empty);
mock.Received(1).SomeEvent += Arg.Any<EventHandler>();
}
/// <summary>
/// <see href="https://fakeiteasy.github.io/" />
/// </summary>
[Benchmark]
public void Event_FakeItEasy()
{
IMyEventInterface mock = A.Fake<IMyEventInterface>();
EventHandler handler = (_, _) => { };
mock.SomeEvent += handler;
mock.SomeEvent += FakeItEasy.Raise.FreeForm.With(null, EventArgs.Empty);
A.CallTo(mock)
.Where(call => call.Method.Name == "add_SomeEvent")
// Expect 2, because raising an event in FakeItEasy is implemented as a call to the add accessor of the event.
.MustHaveHappened(2, FakeItEasy.Times.Exactly);
}
public interface IMyEventInterface
{
event EventHandler? SomeEvent;
}
}
#pragma warning restore CA1822 // Mark members as static