I can do the following to verify if the ErrorOccurred
event of my Consumer
class was raised:
using System;
using FakeItEasy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public interface IService
{
event EventHandler SomethingBadHappened;
}
class Consumer
{
private readonly IService service;
public Consumer(IService service)
{
this.service = service;
service.SomethingBadHappened += (sender, args) => RaiseErrorOccurred();
}
public event EventHandler ErrorOccurred;
protected virtual void RaiseErrorOccurred()
{
var handler = ErrorOccurred;
if (handler != null) handler(this, EventArgs.Empty);
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var service = A.Fake<IService>();
var consumer = new Consumer(service);
bool eventWasRaised = false;
consumer.ErrorOccurred += (sender, args) => { eventWasRaised = true; };
service.SomethingBadHappened += Raise.WithEmpty();
Assert.IsTrue(eventWasRaised);
}
}
I wonder if there is a more "Mocking Framework-y" way of doing this. Something like the below would be nice:
var service = A.Fake<IService>();
var consumer = new Consumer(service);
service.SomethingBadHappened += Raise.WithEmpty();
consumer.ErrorOccurred.MustHaveHappened(/*yada yada/*);
Aucun commentaire:
Enregistrer un commentaire