1

I am trying to write unit tests to make sure that my bindings to view models are being appropriately set to controls within my XAML page.

I have a XAML page structured like this where the Page's DataContext is set to a ViewModel that's passed in when it is navigation to the page occurs (ellipses represent extra properties or controls that aren't related to my issue):

<Page x:Class="MyWin8Proj.Pages.SamplePage" ....>    
    <Page.Resources>....</Page.Resources>    
    <Page.TopAppBar>
        <AppBar ...>
           <customControl DataContext="{Binding}" .../>
        </AppBar>
    </Page.TopAppBar>        
    <Grid ...>
        <ListView x:Name="SelectionList" ItemSource="{Binding ListPropertyInViewModel}"...>...</ListView>
        <Grid DataContext="{Binding SelectedItem, ElementName=SelectionList}"...>
           <TextBlock Text="{Binding SubPropertyOfItem}" /> ...
        </Grid >
    <Grid>
</Page>

In my unit test, I'm executing on the UI thread like I've seen mentioned in other articles/posts (like this one)

        public IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
        {
            return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
        }

        [TestMethod]
        public async Task TestViewModelLoadedIntoContext()
        {
            await ExecuteOnUIThread(() =>
            {
                _targetPage = new MyWin8Proj.Pages.SamplePage();
                _viewModel = new SampleViewModel();
                _targetPage.DataContext= _viewModel;
            });
        }

When the test reaches the new MyWin8Proj.Pages.SamplePage() line, it goes to the Page's constructor, which is simply:

    public SamplePage()
    {
        this.InitializeComponent();
    }

Here, I get thrown an error coming from the TopAppBar in my XAML -

WinRT information: Cannot deserialize XBF metadata property list as 'TopAppBar' was not found in type 'null'. [Line: 0 Position: 0]

Additional information: The text associated with this error code could not be found.

What does this mean?? Is there a way for me to unit test my View's bindings to ViewModels, or is this the wrong approach? Should I approach this an integration test with coded UI tests instead to make sure my bindings are correct?

Community
  • 1
  • 1
Pam
  • 347
  • 4
  • 11
  • Technically a unit test does not involve UI, and it's very rare for a view to be a valid target for a unit test. – Magus Jan 21 '14 at 16:41
  • Your `customControl`, where is it defined? I assume that you simply redacted it. Is the namespace declared properly in your actual test? – Nate Diamond Jan 27 '14 at 02:13
  • @NateDiamond - yes, I'm just putting the customControl in there as a placeholder to demo where it goes. It is properly declared in my actual XAML/test, but the control declaration was not relevant for being able to just test this Page. – Pam Jan 28 '14 at 02:26
  • There is a suggestion for a solution here, although I can't make it work: http://stackoverflow.com/a/28736626/507339 – Nilzor Sep 06 '16 at 16:20

0 Answers0