vendredi 25 août 2017

Testing ICollectionView in WPF ViewModel

I want to write an automated integration test of my ViewModel in a WPF application.

The ViewModel contains an ICollectionView on which a DataGrid is bound in the View. The handling of SelectedItem etc is done with this ICollectionView.

Stripped example of my ViewModel:

public class ViewModel
{
    private List<string> _rows;
    public ICollectionView Collection { get; set; }

    public ViewModel()
    {
        _rows = new List<string> {"1", "2", "3"};
        Collection = CollectionViewSource.GetDefaultView(_rows);
        Collection.MoveCurrentToFirst();
    }

    public string SelectedString => Collection.CurrentItem as string;
}

Problem now is, that in my test, the Collection.MoveCurrentToFirst() method does not result in CurrentItem being set. In the application it does set the CurrentItem. Of course I want to stay as close as possible to the real execution of the application in my tests, so does anyone have an idea how to implement the test in a way, that the CurrentItem is set by the ICollectionView?

Aucun commentaire:

Enregistrer un commentaire