StockTrader.RI Unit Test strategies

Topics: Prism v4 - Silverlight 4
Jan 19, 2011 at 12:30 PM


I have been investigating the test cases of StockTraderRI and came across an interesting situation.

Within the ObservablePositionFixture unit test class, you would find the test method GeneratesModelFromPositionAndMarketFeeds() Which creates a plain new instance of MockMarketFeedService() (which implements IMarketFeedService) to pass it later on into the ctor of a different class.

However within WatchlistViewModelFixture test class, you will find that the same interface is rather being mocked by moqu framework directly rather than to use a real mockimplementation.
Mock mockMarketFeedService = new Mock();

So I wonder, why the one approach isn't taken consistently, which way is better? Do I even need to write a MockService that implements the IMarketFeedService in first place, if I could just have utilized the moqu framework instead for the unit test?

Any pro or contra?

Many Thanks for your advice,

Jan 19, 2011 at 6:33 PM

Hi Houman,

One of the possible reasons why the team could have chosen that approach might be that using a mock implementation could be easier when the mock has to have a complex behavior, while using Moq could simplify and fasten the creation of simpler mocks.

Another possibility would be that the mocks present in the previous versions of Prism (which were made when Moq wasn’t being used on Prism) are being reused in this version, since they fulfill the requirement for testing the components in the Reference Implementation.

There is no guidance on Prism regarding whether to use Moq or a mock implementation when unit testing. For an overview of the capabilities of Moq you could check its Codeplex Project.

I hope you find this information useful.


Miguel Bronzovic


Jan 20, 2011 at 9:55 AM

Thanks Miguel,


That makes sense. I will look further into Moq and see how to make the testing easier.


Jan 21, 2011 at 5:47 AM

Let me add a little insight here from the team...

The Prism v4 release built on quite a bit of code that was written over several project cycles.  In earlier cycles, hand-written mocks were used extensively.  For Prism v4, after working with the MS legal team, it was determined that we could use Moq for the project.  We did so, but we intentionally did not invest the time to convert all unit test projects to use Moq, as there were other pieces of guidance that were much more important (and with time constraints and a small team, these decisions always get interesting).  Instead, when we updated a class in Prism, we selectively converted hand written mocks one test fixture at a time.  The idea was that over time, the unit tests would eventually all be converted, but it would take longer than one release.

The way I see it, both hand written and framework created mocks are valuable in different situations, and if you write your code in a testable way (following a lot of the principles that TDD helps enforce), it should be easy to switch a hand-crafted mock for a framework generated one easily.

I hope that helps,
Michael Puleio