Take the 2-minute tour ×

I'm attempting to test an async process where multiple requests are made to a service with different values which result in multiple completion events being raised.

The app is WP7 application with the code & tests in WP7 Class Library projects, all other tests (with mocks) works fine it is only this test I cannot get to work.

The test is:

    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);


        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" }));


        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);

The test is failing because the second event isn't fired instead the first raise fires twice.

What am I missing?

share|improve this question
are you sure the GetStock method is getting called twice with different parameters? –  AD.Net Aug 8 '12 at 20:07
Yes. In the test you'll see that 2 stocks get added to the Stocks collection, the refresh method does a for loop over the collection calling the GetStock method you see in the fake. When I debug the code I see 2 calls to GetStock with the correct values but only the MSFT mock event is raised for both –  Nathan Aug 8 '12 at 20:39
You might try queueing the event args since you pretty much know what'll be the order of calls to the GetStock method. –  AD.Net Aug 8 '12 at 20:48
Speaking of queuing, here's a blog post about returning different results on successive method calls. Perhaps you could adapt the ReturnsInOrder extension method to your situation, i.e. call Raises instead of Returns. /cc @AD.Net –  Andrew Aug 8 '12 at 21:49
There is nothing wrong with the event Raises setup it is working as expected, and I'm not able to repro your issues I get different event args for the different invocations. So I guess the problem is somewhere in your implementation. Can you post your Refresh method and how and where do you subscribe on the StockQuoteReceived event or maybe the full StockSummaryViewModel? –  nemesv Aug 13 '12 at 12:41
add comment

Your Answer



Browse other questions tagged or ask your own question.