Best way for unit test events using NUnit and Moq?

I use NUnit and Moq to test a class that has some events, and I'm trying to find a better way to check if an event was fired. I came up with this solution, but it looks a little dirty, since I have to create an interface for the test. Anyway, can I do the same with less code or not create an interface?

This is not so bad, but I feel that someone might have a better solution. Any ideas are welcome. Thanks.

[Test] public void StartedAndStoppedEventsShouldFireWhenStartedAndStopped() { var mockStartedEventSubscriber = new Mock<IEventSubscriber>(); var mockStoppedEventSubscriber = new Mock<IEventSubscriber>(); _NetworkMonitor.Started += mockStartedEventSubscriber.Object.Handler; _NetworkMonitor.Stopped += mockStoppedEventSubscriber.Object.Handler; _NetworkMonitor.Start(); _NetworkMonitor.Stop(); Func<bool> func = () => { return (eNetworkMonitorStatus.Stopped == _NetworkMonitor.Status); }; Utilities.WaitUntilTrue(func, _NetworkMonitor.Interval * 2, 10); _NetworkMonitor.Started -= mockStartedEventSubscriber.Object.Handler; _NetworkMonitor.Stopped -= mockStoppedEventSubscriber.Object.Handler; mockStartedEventSubscriber.Verify(h => h.Handler(_NetworkMonitor, EventArgs.Empty), Times.Once()); mockStoppedEventSubscriber.Verify(h => h.Handler(_NetworkMonitor, EventArgs.Empty), Times.Once()); } public interface IEventSubscriber { void Handler(object sender, EventArgs e); } 
+6
source share
3 answers

This test seems easier to do without layouts. Double test device as event subscriber.

 _networkMonitor.Started += this.SetStartedFlag; // a private method which sets a flag in the test fixture. _networkMonitor.Start(); Assert.That(StartedFlag, Is.True); 
+4
source

I think you don't need a mok at all for this. You can simply register for events and evaluate if they were fired (or the number of times they were fired):

 public class NetworkMonitor { public event EventHandler Started; public event EventHandler Stopped; public void Start() { var handler = Started; if (handler != null) handler(this, EventArgs.Empty); } public void Stop() { var handler = Stopped; if (handler != null) handler(this, EventArgs.Empty); } } [Test] public void StartedAndStoppedEventsShouldFireWhenStartedAndStopped() { NetworkMonitor classUnderTest = new NetworkMonitor(); bool startedWasFired = false; int stoppedWasFired = 0; classUnderTest.Started += (o, e) => { startedWasFired = true; }; classUnderTest.Stopped += (o, e) => { stoppedWasFired++; }; classUnderTest.Start(); Assert.That(startedWasFired); classUnderTest.Stop(); Assert.That(stoppedWasFired, Is.EqualTo(1)); } 
+3
source

Below is a snippet of the path that I used in the past for a good effect. It just adds (in my case) ConnectionChangedEventArgs instances to the <> list every time the event fires. It then claims how many events were fired. I hope you get this idea and can adapt it to your needs if you want.

 [Test] public void GoodConnectionRaisesConnectionChangedEvent() { const int EXPECTED = 1; List<ConnectionChangedEventArgs> ev = new List<ConnectionChangedEventArgs>(); // Mocks and setup stuff here... using (PlcController pc = new PlcController(mock.Object)) { pc.ConnectionChanged += delegate(object sender, ConnectionChangedEventArgs e) { ev.Add(e); }; pc.Connect(); } Assert.That(ev.Count, Is.EqualTo(EXPECTED)); } 
0
source

Source: https://habr.com/ru/post/891684/


All Articles