2
votes

I developed an API which aims to get the location of the user and stuff like that.

I would like to test my code, so I created a Windows Phone 8 Unit Test project. However, it seems that the tests which I can do are very limited, because:

  • I can't switch off/on network, GPS... => to test cases like the user doesn't have a location service enables on his phone;
  • I can't emulate a location. The only default location I get is the default one of the emulator (near Seattle)

Is there a way to make some more accurate tests ?

3

3 Answers

1
votes

Unit testing consists of testing your code in complete isolation. When you have code that interacts with external resources (e.g., Location Services), you need to design seams into your architecture which will allow you to mock out that external code.

You mentioned testing code that could behave differently when GPS is enabled/disabled. This is a good example of where a seam could be introduced. For example, consider that you had the following code:

var watcher = new GeoCoordinateWatcher();
watcher.Start();
if (watcher.Permission.Equals(GeoPositionPermission.Granted))
{
    // Do stuff that requires GPS
}
else
{
    // Handle situation where GPS is disabled
}

You aren't able to test this code in isolation because it is tightly coupled to the Location Services (specifically, the GeoCoordinateWatcher class). To address this, you can introduce an abstraction with the following interface:

public interface IGpsChecker
{
    bool IsGpsEnabled();
}

Using this interface, your code could be changed to:

if (m_gps_checker.IsGpsEnabled()) // m_gps_checker is supplied to your class via constructor, etc.
{
    // Do stuff that requires GPS
}
else
{
    // Handle situation where GPS is disabled
}

In your production code, you would provide the class with an implementation that simply uses the original logic:

public class LocationServicesGpsChecker : IGpsChecker
{
    public bool IsGpsEnabled()
    {
        var watcher = new GeoCoordinateWatcher();
        watcher.Start();

        return watcher.Permission.Equals(GeoPositionPermission.Granted);
    }
}

However, in your test code you would provide a test double to the class that you're testing, giving you precise control of the conditions under which you're working.

0
votes

The emulator error youre experiencing is a well known one.

Here is the work around.

Launch your app

Press the start button

Open the emualtors Maps app

Set your location with the emulators GPS location tool

find your location with the Maps app

Return to your app

Now you can use the built in visual studio network emulation tool

In most cases if you continue to update your location on the emulators GPS simulation tool it will continue to work. On occasion you will have to repeat steps 2 -> n

0
votes

As with any other unit tests, you may have to "mock" these scenarios:

  1. You may have to "mock" the scenario of location services not available.

  2. You may have to prepare a hard coded set of coordinates (longitude/latitude) values to run your test with.

Hope this answers your question.