State of unit testing for Windows Phone

前端 未结 6 1183
抹茶落季
抹茶落季 2021-02-04 03:16

I\'ve been pushing my Google Fu to the limits trying to find the most recommended / stable setup for doing TDD + CI for Windows Phone applications. Can anyone who has successful

6条回答
  •  天涯浪人
    2021-02-04 03:59

    I think a matter of this has to due with how you write your tests.

    • Eventually some code will have to touch some phone specific things. Those should be isolated as dependencies and faked out. I haven't figured out a proper way to get the unit tests to actually run within the phone itself. Unfortunately, those dependencies remain untested for me.
    • Using NUnit for Silverlight allows you to get assertions: http://code.google.com/p/nunit-silverlight/
    • I've used Resharper to run the unit tests without issue. Similarly, you can use the nunit-console to run the tests and get XML output.
    • Ayende's Rhino Mocks for Silverlight work fine for mocking / stubbing dependencies.
    • Continuous integration was a bit tricky. The WP7 SDK is not available for on the Server platform, so I built a new one on Windows 7 for my CI. There may be ways around that limitation, but I didn't bother.

    The other tool you will want is MVVMLight. This will allow you to use EventTrigger and ICommand instead of events, since testing the events is significantly more work and can't be bound through the DataContext.

    As far as how I designed my application:

    The ViewModel can take in any number of dependencies, which get resolved using MicroIoC.

    The actual code behind of the XAML resolved the ViewModel and sets it to the data context. This is unfortunate because that means you can't set the DataContext in XAML, but was a trade off I was willing to accept for dependency injection, like this:

    public partial class SignUpPage
    {
        public SignUpPage()
        {
            InitializeComponent();
            DataContext = IoC.Resolve();
        }
    }
    

    Fortunately, that's the only C# code that actually appears in my XAML code behind. From there, it's fairly regular MVVM with using binding and the DataContext.

    Now you can test your ViewModel, inject the required dependencies (or fake them out) and it'll run fine without being in the emulator, so long as you don't try to use something WP7 specific.

提交回复
热议问题