Create a Region Business Object Test Class

Purpose:

This class will contain at least one test for each property and method of the Region business object, and it will serve as a model for classes intended to test business objects.  These tests will exercise every line of code written so far in this class.

Starting Point:

The Region business object must be complete, and the Business Object Tests project must have been added to the solution.  A test data dataset should have been generated, and a sample test data file should have been created.

Steps:

  1. Add a new class named RegionTest to the Business Object Tests project.
  2. Import the business objects project and the NUnit.Framework library to your class.
  3. Create the following Regions in the body of your new class, Constructors, Public Methods, and Private Methods.  Constructors’ on the next to mark the end. Then move the default constructor inside of this region.
  4. You will need a dataset to hold the test data file, and a data row to hold the current set of test values.  Declare a RegionTestData dataset and a RegionTestData.RegionRow data row in the private Variables region.   Initialize the dataset variable as shown  below:

    #region Private Variables
        RegionTestData _testDataset = new RegionTestData();
        RegionTestData.RegionRow _testRow;
    #endregion Private Variables
     
  5. Move the default constructor into the Constructors region and modify it so that it expects a parameter that is the name of the XML file containing the test data.  Call the dataset’s ReadXML method and pass it the datafile name.  If your XML file is not linked to your schema, you must also use the InferSchema option, like so:

     #region Constructors
     public RegionTest(string nameOfDataFile)
    {
        _testDataset.ReadXml(nameOfDataFile, System.Data.XmlReadMode.InferSchema);
    }
    #endregion Constructors

    Note that this line will throw an error if you try to read in a file that does not match the dataset’s schema.  Note also that this forces a user of these tests to provide a data file before any of the tests can be run (as we intended).
  6. Create a private method called CreateTheObject.  This method should create a Region object, populate it with data from the test row, and save it.
  7. Create a private method called ReadTheObject.  This method should read the Region object created in the previous step using the primary key to find the correct record.   It should then compare the values that were actually stored against the values that were intended to be stored.
  8. Create a private method called UpdateTheObject.  This method should read the Region object created in the previous step using the primary key to find the correct record.   It should then update the update values from the test data row and save the changes.  Finally, it should read the values back from the database, verifying them in much the same way that the Read function was tested. 
  9. Create a private method called DeleteTheObject.  This method should delete the Region object updated in the previous step and verify the deletion.  )
  10. The next step is to create a private method called RunTheTestCases that will iterate through the dataset taking each row in turn.  Create a foreach loop that selects out each Region row in the dataset.  As each row is retrieved, it will run each of the tests described above. 
  11. Create another private method called TestTheDataset.  This method will test the public static method that returns all of the rows and columns of the Region table. 
  12. Create a public method that will run all of the tests by calling the TestTheDataset, then RunTheTestCases.  It should look something like this:

    public void RunAllTests()
    {
        TestTheDataset();
        RunTheTestCases();
    }
     
  13. Compile and Save.

Rationale:

This set of tests exercises every line of code that we have written, and it verifies the accuracy and completeness of every basic service provided by the business object and its supporting classes.  In addition, it addresses the need to easily expand the number of test cases by accepting data in an XML format.   This format, as we have seen time and time again, works very well with Microsoft's datasets.

Discussion:

A test class is surprisingly simple to create.  The design is fairly obvious because the job of the test class is to test everything that the business object does.  The important thing is that you want to test your business object by using it in much the same way as your customer (the user interface programmer) will use it.

This would be typical of a business object where the creator provides the primary key.  How would the design have to change if the key value was automatically generated by the database?  (Tip: that would largely depend on how the design of your business object changes.)

Previous Step: Create the Business Objects Test Project

Next Step: Run the First Tests