我正在尝试测试一个异步进程,其中向具有不同值的服务发出多个请求,这会导致引发多个完成事件。
该应用程序是 WP7 应用程序,其中包含 WP7 类库项目中的代码和测试,所有其他测试(带有模拟)都可以正常工作,只有这个测试我无法开始工作。
测试是:
[Test]
public void Should_referesh_all_stocks_held_in_collection()
{
var fakeStockService = new Mock<IStockService>();
var msftStock = new StockViewModel { Symbol = "MSFT", Name = "Microsoft", Price = 12.5m, Change = "+1" };
var googStock = new StockViewModel { Symbol = "GOOG", Name = "Google", Price = 12.0m, Change = "-1" };
var target = new StockSummaryViewModel(fakeStockService.Object);
target.Stocks.Add(msftStock);
target.Stocks.Add(googStock);
fakeStockService.Setup(s => s.GetStock("MSFT"))
.Raises(s => s.StockQuoteReceived += null, new StockQuoteEventArgs(new QuoteReceived { Symbol = "MSFT", Name = "Microsoft Corp", Last = "13.5", Change = "+1" }));
fakeStockService.Setup(s => s.GetStock("GOOG"))
.Raises(s => s.StockQuoteReceived += null, new StockQuoteEventArgs(new QuoteReceived { Symbol = "GOOG", Name = "Google", Last = "11.5", Change = "-0.5" }));
target.Refresh();
Assert.AreEqual(2, target.Stocks.Count);
var stock = target.Stocks.Single(s => s.Symbol == "MSFT");
Assert.IsTrue(stock.Change == "+1" && stock.Price == 13.5m);
stock = target.Stocks.Single(s => s.Symbol == "GOOG");
Assert.IsTrue(stock.Change == "-0.5" && stock.Price == 11.5m);
}
测试失败,因为没有触发第二个事件,而是第一个 raise 触发了两次。
我错过了什么?