Workshop 04 - Testing

  Home 

Implementing the Data Access Layer

In the UPBLib project add a new class, named MatrixDataAccess, for implementing Matrix comma separated values (csv) file persistence.
In UPBLib, add a folder named "Contracts". In this new folder add a new interface named IMatrixDataAccess: Solution Explorer\ Right click on the Contracts folder\ Add\ New Item\ Interface
In the Matrix class, add an overloaded constructor which expects an array of strings as parameter. The Csv static class is available here: csv file

Add also a method for exporting the matrix to a csv string:

The MatrixDataAccess class will implement the IMatrixDataAccess interface:

A loosely coupling (vs strongly coupling) between Matrix and MatrixDataAccess classes will be considered:

Automatic Testing

When testing a gear, one would first test each component wheel => unit testing
Only after confirming each wheel has no defect, one could consider testing the interaction between wheels => integration testing

Unit Testing

As suggested, we intend to test each class in isolation of any dependency.
As Matrix and MatrixDataAccess are loosely coupled, it is possible to use polymorphism for injecting a data access "mock" into the Matrix class.
Add a new class, named MatrixDataAccessMock : IMatrixDataAccess into the UPBTest test project.

In order to be able to compare 2 Matrix instances (contained data, not addresses), override the Matrix Equals virtual method:

It is the time to write our first unit test into the MatrixTest test class. Notice the [TestClass] and [TestMethod] attributes used!

For checking the behaviour in case incorrect input parameters are provided, one could consider implementing negative tests:

The test methods are often sharing common initialization actions. In order to increase code reusability, one could implement a test initialization method => a function decorated with the TestInitialize attribute.
Notice that test initialization will be executed before calling each test method and not once per class.

Integration Testing

For implementing integration tests, one could use the MatrixDataAccess class (not the Mock) together with a corresponding set of testing files.

References:

Migration of hyper-fractal analysis from visual basic 6 to C# .Net

  Home