Thursday, September 29, 2011

how to collect the names of only string value columns from a DataTable, how to test an abstract class, and when to nest two classes in one file

ListViewModel is an abstract class that has a method called ReturnListOfNamesOfStringBasedColumns in it which has one line within it:

return this.ListTable.Columns.Cast<DataColumn>().Where(column => column.DataType == typeof (string)).ToDictionary(column => column.ColumnName, column => column.ColumnName);

 
 

I am testing it like so:

[TestClass]

public class ListViewModelTest

{

   [TestMethod]

   public void ReturnListOfNamesOfStringBasedColumns_Behaves_As_Expected()

   {

      //arrange

      PseudoChildForTestingAnAbstractClass listModel =

            new PseudoChildForTestingAnAbstractClass();

      listModel.ListTable = new DataTable();

      listModel.ListTable.Columns.Add("Foo", typeof (string));

      listModel.ListTable.Columns.Add("Bar", typeof (Int32));

      listModel.ListTable.Columns.Add("Baz", typeof (string));

      listModel.ListTable.Columns.Add("Qux", typeof (string));

      

      //act

      Dictionary<string, string> dictionary =

            listModel.ReturnListOfNamesOfStringBasedColumns();

      

      //assert

      Assert.AreEqual(3, dictionary.Count);

      Assert.AreEqual("Foo", dictionary["Foo"]);

      Assert.AreEqual("Baz", dictionary["Baz"]);

      Assert.AreEqual("Qux", dictionary["Qux"]);

   }

}

 

public class PseudoChildForTestingAnAbstractClass : ListViewModel<object>

{

 

}

 
 

This is a rare case where it seems OK to have two classes in one file. Another example might be having an Enum class next to the only class that is ever going to use it.

No comments:

Post a Comment