我正在尝试测试一个异步进程,其中对具有不同值的服务发出多个请求,这会导致多个完成事件被引发。
该应用程序是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);
}
测试失败,因为没有触发第二个事件,而是第一次点火两次。
我错过了什么?