Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
545
Unit tests with xamGrid as test case.
posted

Hi!

I am trying to unit test some code which manipulates a xamGrid. However, never fills with rows or columns even though I set an ItemsSource on it. This is my unit test. How can I create a XamGrid with rows and columns for testing? By the way, my project is in a WPF context, not Silverlight, but this is the only place with a XamGrid forum.

[TestFixture]
public class XamGridUtilsTests
{
   private class DataItem
   {
     public int I { get; set; }
     public string Text { get; set; }

     public DataItem(int i, string text)
     {
       I = i;
       Text = text;
     }
   }

   [Test]
   [RequiresSTA]
   public void SelectAllRows_NoneSelected_SelectsAllRows()
   {
     // Arrange
     ObservableCollection<DataItem> items = new ObservableCollection<DataItem>();
     for (int i = 1; i < 4; i++)
     {
       items.Add(new DataItem(i, "Column" + i));
     }

     XamGrid grid = new XamGrid();
     grid.AutoGenerateColumns = true;
     grid.ItemsSource = items;

     CollectionAssert.IsNotEmpty(grid.Rows);       <-- THIS FAILS
     CollectionAssert.IsNotEmpty(grid.Columns);  <-- THIS FAILS

     // Act
     XamGridUtils.SelectAllRows(grid);

     // Assert
     foreach (var row in grid.Rows)
     {
       Assert.IsTrue(row.IsSelected);
     }
   }
}

Parents
  • 6759
    Suggested Answer
    Offline posted

    Hi Atle Rudshau,

    as far as I could understand you do not attach the grid to a visual tree. There is huge chance that XamGrid needs to be loaded/measured in order to try to auto-generate its columns and populate the rows.

    HTH,

Reply Children