Windows Phone development and Unit Testing

前端 未结 4 1529
攒了一身酷
攒了一身酷 2020-12-25 10:23

I\'m looking to start a project targeting Windows Phone, but I\'m slightly put off by the lack of unit testing support[1].

I\'m used to using NUnit/ XUnit for most o

相关标签:
4条回答
  • 2020-12-25 10:44

    Update: Unit testing for Windows Phone 8 is supported now.

    http://blogs.msdn.com/b/visualstudioalm/archive/2013/01/31/windows-phone-unit-tests-in-visual-studio-2012-update-2.aspx

    0 讨论(0)
  • 2020-12-25 10:49

    I managed to unit test my viewmodels used in a VS2013 WP8.0 application, using Moq. Here is my solution structure: - WP8 (Silverlight) with a home made Prism library - WP8 Portable Library as my Presentation layer - WP8 Unit tests project

    Go here and download the latest Moq version. In the the Silverlight folder, you'll find 2 dlls, one for Moq and one for Castle Core. Use them as reference in your unit test project. Note that VS2013 won't allow you to directly add those DLL as reference. So unload your project, edit the .csproj, manually add the 2 references and reload your project

    Not a very clean way, but it's the only way i found to unit test my viewmodels.

    0 讨论(0)
  • 2020-12-25 10:57

    Here's what we do:

    • Abstract phone-specific logic into facades
    • Inject dependencies wherever possible
    • Avoid using any code that requires to run on the UI thread upon construction, this meaning any UI control, ImageSource, basically any System.Windows type that isn't a enumeration (such as Visibility)
    • Heavily rely on ValueConverters to convert business logic into UI logic.

    If you do this, then you can use the the build-in Visual Studio unit testing framework, to test Windows Phone projects (not just Portable Class Libraries) with a bit of hackery.

    1) First step is to disable strong name validation , this can be done with following Powershell script:

    reg DELETE "HKLM\Software\Microsoft\StrongName\Verification" /f
    reg ADD "HKLM\Software\Microsoft\StrongName\Verification\*,*" /f
    if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64")
    {
       reg DELETE "HKLM\Software\Wow6432Node\Microsoft\StrongName\Verification" /f
       reg ADD "HKLM\Software\Wow6432Node\Microsoft\StrongName\Verification\*,*" /f
    }
    Restart-Service msiserver
    

    Notice that it creates a security vulnerability on the machine this is applied to!

    2) Second step is to create a Visual Studio Unit Test Project, a regular one, not a Windows Store Unit Test Project.

    3) Third step is to include the Windows Phone 8 project as a reference. This works, and you can now use Microsoft Fakes to make mscorlib and System.dll , allowing you to fake types like DateTime in your unit tests, a very handy feature.

    4) Fourth step is to get a local copy of the following Windows Phone assemblies (and any additional you need):

    • System.Windows
    • Microsoft.Phone

    They can be found in the C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\MDILXAPCompile\Framework folder.

    Copy them locally to a /lib/ folder, and then unload and edit the MSBUILD xml of your Unit Test Project.

    Change:

    <Reference Include="System.Windows" />
    <Reference Include="Microsoft.Phone" />
    

    To

    <Reference Include="System.Windows, Version=2.0.6.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, processorArchitecture=MSIL">
      <HintPath>lib\System.Windows.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Phone, Version=8.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e, processorArchitecture=MSIL">
      <HintPath>lib\Microsoft.Phone.dll</HintPath>
    </Reference>
    

    Additionally to avoid a warning when compiling, add following element to the first <PropertyGroup>

    <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
    

    Reload, compile, run the tests. Voilà, and you're done. You can even integrate this with the Microsoft Code Coverage functionality.

    NB: Unit Testing is not officially supported by the Windows Phone Division, this is a workaround found by my team and I at the Skype Division.

    Bonus answer:

    Does anyone know if you can target Windows Phone 7.x using the WP8 SDK?

    enter image description here

    0 讨论(0)
  • 2020-12-25 10:58

    I've done some fairly basic tests and found that a Portable Library Class project and a normal Class Library for the tests work well.

    The caveat to this is that I've yet to find an MVVM framework built for portable class libraries, making the View Models untestable...unless...you use file linking (and assuming the View Models inherit from an MVVM framework specific base class). P While its not ideal, it's the best way forward I've found.


    EDIT:

    It seems as though there's a Prism (v4.5) release that's built on Portable Class Libraries...

    It's not, at the time of writing at least, on NuGet in either stable or pre-release, but I'll do some investigation into its usage and update.

    I've also had word that the Windows Phone 8 SDK will be able to target Windows Phone 7.

    0 讨论(0)
提交回复
热议问题