I want to unit test if an event raised by a dependency being subscribed by a class under test. To set the context, I have the below interfaces and classes.
ITestedService.cs
public interface ITestedService
{
Task Start();
Task Stop();
}
IDependency.cs
public interface IDependency
{
event EventHandler<SoAndSoEventArgs> SomethingHappened;
Task Start();
Task Stop();
}
ISecondDependency
public interface ISecondDependency
{
Task DoYourJob(SoAndSo soAndSo);
}
TestedService.cs
public class TestedService : ITestedService
{
readonly IDependency m_dependency;
readonly ISecondDependency m_secondDependency;
public TestedService(
IDependency dependency,
ISecondDependency secondDependency)
{
m_dependency = dependency;
m_secondDependency = secondDependency;
}
public async Task Start()
{
m_dependency.SomethingHappened += OnSomethingHanppened;
await m_dependency.Start();
}
private async void OnSomethingHanppened(object sender, SoAndSoEventArgs args)
{
SoAndSo soAndSo = SoAndSoMapper.MapToDTO(args);
await m_secondDependency.DoYourJob(soAndSo),
}
}
With the above context, I want to Unit test Start()
method of the TestedService
class using xUnit
.
I want to know how I can:
- Assert if the event is attached to a handler.
- Simulate the event
IDependency.SomethingHappened
being fired. - Verify if the
OnSomethingHappened
method is executed - Verify if the
ISecondDependency.DoYourJob(soAndSo)
is called.
SomethingHappened
event of them_dependencyMock
instance. Might this help? It shows how to usem_dependencyMock.Raise
. – Mottlem_dependencyMock.Raise(
is part of the act then ryt? @ZevSpitz – Authenticity