インターフェイスの実装で使用されているイベントが、モックしているインターフェイスに表示されない場合に、moqを使用してイベントを単体テストで発生させる方法はありますか?
注:私のインターフェースはUIとは何の関係もなく、イベントはUI通知にのみ使用されるため、リポジトリはクライアント/ UIとは別のライブラリにあるため、実際のインターフェースからその動作を切り離したいと思いました。
例えば:
[Test]
public void TestRaiseBarProcessed()
{
ManualResetEvent barProcessedEvent = new ManualResetEvent(false);
bool called = false;
//Arrange
Mock<IFooRepository> mockFooRepository = new Mock<IFooRepository>();
mockSourceRepository
.Setup(a => a.SearchForBar(barsToFind))
.Returns(barsFound)
.Raises(
a => a.BarProcessed += null,
new BarFoundEventArgs(It.IsAny<string>()));
IList<IFooRepository> mockFooRepositories =
new List<IFooRepository>();
mockFooRepositories.Add(mockFooRepository.Object);
FooBar fooBar = new FooBar(mockFooRepositories, FooList);
fooBar.CurrentBarBeingProcessedInfo += (sender, e) =>
{
barProcessedEvent.Set();
called = true;
};
//Act
fooBar.CallFooRepositoryMethod();
barProcessedEvent.WaitOne(25, false);
//Assert
mockFooRepository.Verify(
a => a.SearchForBar(barsToFind),
Times.Once());
Assert.AreEqual(true, called);
}
これについてさらに説明が必要な場合はお知らせください。