Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

During the development of our CodeSmith CSLA templates we have spent a lot time unit testing the generated code to ensure everything was working correctly.

Image Removed

In the future we would like to generate basic CRUD unit tests for any project. We decided to unit test the PetShop database to go along with our PetShop Sample application. We have a solution both in VB and C# that can be found in (Documents\CodeSmith\Samples\v5.2\Projects\Framework-Samples\Csla) when you install CodeSmith or grab the latest version from SVN. We have unit tests for all the Data Access implementations as well as collections. We test primarily against two tables, Category and LineItem.

Image RemovedDuring the development of the CSLA templates, we thought what better way to test the templates than to write a sample application. So we wrote a working PetShop Sample application in C# and Visual Basic that can be downloaded here

Info
Please make sure that you update the connection string to point to a local PetShop database instance.

Image Added

We took the PetShop sample applications a step further by writing unit tests against the generated code. You can find unit tests for all the business object types and Data Access Layer implementations. This is a great starting point for learning more about the generated code.

Image Added

These NUnit tests will give you a good understanding of how different operations are performed in CSLA. Feel free to submit to CodeSmith contact support or  or forums any unit tests you think we don't have.  If you find a bug in the CSLA templates, its a huge help to the community and us if you submit a failing unit test for the PetShop sample application that can reproduce your bug. This ensures we have more testability and more test ability and no regressions in future versions. However, any unit test will be appreciated along with the respected sample schema.

When contributing new unit tests, please try to follow our common testing pattern for consistency that outputs a description and how long it took to run the test. Image RemovedBelow is an example of one of our unit tests.

Code Block
languagecsharp
public void Step_01_Insert_Duplicate()
{
    Console.WriteLine("1. Testing Duplicate Records.");
    Stopwatch watch = Stopwatch.StartNew();
    //Insert should fail as there should be a duplicate key.
    Category category = Category.NewCategory();
    category.CategoryId = TestCategoryID;
    category.Name = TestUtility.Instance.RandomString(80, false);
    category.Description = TestUtility.Instance.RandomString(255, false);
    try
    {
        Assert.IsTrue(category.IsValid, category.BrokenRulesCollection.ToString());
        category = category.Save();
        // Fail as a duplicate record was entered.
        Assert.Fail("Fail as a duplicate record was entered and an exception was not thrown.");
    }
    catch (Exception)
    {
        Assert.IsTrue(true);
    }
    Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
}