Use of Microsoft Pex with Prism composite application

282 views Asked by At

I'm implementing unit testing in my Prism composite application and trying to implement Microsoft Pex (http://research.microsoft.com/en-us/projects/pex/) to speed up this task. But I'm experiencing a lot of troubles linked to Prism. Despite the use of Moles to stub Prism implementations, I have a lot of errors and problems doing it.

Has someone already successfully used Pex with a Prism application?

Thanks in advance for your answers

1

There are 1 answers

0
Paul On BEST ANSWER

After hours of struggle, I finally managed to implement tests in the project I wanted. Here are some details about my experience of Pex and Moles (I also use CodeContracts and I advise it, but this has no incidence here) with a Prism composite application.

  • Mainly two things are touchy with Pex and Moles: .Net framework classes and interfaces, and Prism classes and interfaces. They are not easy to instrument because of their low-levelness (system) and the dependency injection (Prism).
  • The first thing I did was running a first time Pex on my project to make errors appear. There, I found a successful test that I asked Pex to generate. It automatically creates the test project with Pex references.
  • There, I identified which classes and interfaces had to be stubbed: It concerns mainly Prism interfaces like IEventAggregator and IRegionManager. These interfaces have implementations and can be stubbed by Moles. All needed is right click in the Prism reference (in project References) and click "Add moles for Prism". Auto-generate an assembly containing stubs for all Prism classes, automatically used by Pex at the next exploration. Be careful with this tool. My first reaction was to "moles" everything (Prism, System, dependencies..). This is death for the test project. I didn't find out how to suppress moles assemblies. So when it's done, it's done!
  • I advise to "mole" dependencies part by part, and to think before doing it. Sometimes an object cannot be created by Pex because of an underlying parameter bad instanciated, causing objecg creation to fail (Pex report doesn't show the difference between a lack of implementation and an error in a lower-level. It always says: "MyClass object couldn't be created" and suggest to create a factory)
  • I created factories for classes that couldn't be stubbed by Moles. Especially, it doesn't work for events working with event aggregator (inheriting from CompositionPresentationEvent).
  • Be patient and resolved. I find Pex still experimental and "no-way-back". Best way to solve rebellious error messages was to delete the test project and to start over with pex exploration.

If someone can help my answer and give some more details and correction, it would be more than welcome!