Testing Windows Phone 7 Apps with Typemock

The following is a guest post from Ignacio Raffa, a student in Argentina, participating in Microsoft’s worldwide competition for students, the Imagine Cup 2012. He is a Microsoft Student Partner and Windows Phone 7 Developer.

We are developing an open source Windows Phone 7 application that makes extensive use of the device Accelerometer.

To make a proper use of the Accelerometer in the application, we implemented an interface to abstract the concrete class (Microsoft.Devices.Sensors.Accelerometer). We have the significant methods and event of that class in the following interface:

  1. C#
  2. public interface IAccelerometerService
  3. {
  4. void Start();
  5. void Dispose();
  6. void Stop();
  7. event EventHandler<AccelerometerServiceEventArgs> ReadingChanged;
  8. }
  9. The AccelerometerServiceEventArgs is a custom class that we made (prior to using Typemock Isolator) that enables us to “fake” an accelerometer reading.
  10. The problem is that, when the real accelerometer fires the original ReadingChanged event, the argument is of type AccelerometerReadingEventArgs (a sealed final class with not overrideable properties).
  11. And that is our first introduction to Typemock Isolator. We have easily created a Unit Test faking an instance of the AccelerometerReadingEventArgs class.
  12. C#
  13. [TestMethod]
  14. [Isolated]
  15. public void TestAccelerometerMock()
  16. {
  17. var accService = new AccelerometerService();
  18. var eventExec = false;
  19. accService.Start();
  20. accService.Stop();
  21. accService.ReadingChanged += (a, e) =>
  22. {
  23. eventExec = true;
  24. };
  25. var rm = Isolate.Fake.Instance<AccelerometerReadingEventArgs>(Members.ReturnRecursiveFakes);
  26. Isolate.WhenCalled(()=>rm.X).WillReturn(1);
  27. Isolate.WhenCalled(() => rm.Y).WillReturn(1);
  28. Isolate.WhenCalled(() => rm.Z).WillReturn(1);
  29. accService.accelerometer_ReadingChanged(accService, rm);
  30. accService.Dispose();
  31. Assert.IsTrue(eventExec);
  32. }
  33. We are able to fake the values of X, Y and Z properties using Members.ReturnRecursiveFakes property of the Isolate.Fake.Instance constructor.
  34. With Typemock Isolator, we have 100% of Code Coeverage for all the classes that we developed, with a little of effort.

     

    Like what you saw? Want to try unit testing in Windows Phone 7, .NET, or C++ with Typemock Isolator. Download it for free.