NCover to Cover Your A@#!
NCover is a code coverage tool that helps to point out how much code is covered under the unit tests. The idol code coverage is 100% which means all the method used in the domain layer are being covered in the test. Lets create a small sample to see NCover in action.
I have a simple Customer class as shown below:
public class Customer
{
private int _id;
private string _firstName;
private string _lastName;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
And the CustomerService class:
public
class CustomerService {
public const string connectionString = "Server=localhost;Database=School;Trusted_Connection=true";
public static void AddCustomer(Customer customer)
{
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("usp_InsertCustomer", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@FirstName", customer.FirstName);
myCommand.Parameters.AddWithValue("@LastName", customer.LastName);
myCommand.Parameters.Add("@ReturnValue", SqlDbType.Int, 4);
myCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.ReturnValue;
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
customer.Id = (int) myCommand.Parameters["@ReturnValue"].Value;
}
public static Customer GetCustomerById(int id)
{
Customer customer = null;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("SELECT CustomerID, FirstName,LastName FROM Customers WHERE CustomerID = @Id", myConnection);
myCommand.Parameters.AddWithValue("@Id", id);
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
customer = new Customer();
customer.Id = (int) reader["CustomerID"];
customer.FirstName = (string)reader["FirstName"];
customer.LastName = (string)reader["LastName"];
}
reader.Close();
myConnection.Close();
return customer;
}
}
Finally here is the CustomerFixture class which includes a CanAddCustomer test.
[Test]
[RollBack]
public void CanAddCustomer()
{
Customer customer = new Customer();
customer.FirstName = "John";
customer.LastName = "Doe";
// Add a new customer
CustomerService.AddCustomer(customer);
Customer cVerify = CustomerService.GetCustomerById(customer.Id);
Assert.IsTrue(cVerify.Id != 0,"Id is not correct");
Assert.AreEqual(cVerify.FirstName, customer.FirstName);
Assert.AreEqual(cVerify.LastName, customer.LastName);
}
Now, if I run NCover on my application I get the 100% coverage since both of my methods in CustomerService as well as the properties of the Customer class are being used in the tests.

Off course, you can fool NCover by just including the methods in the test and not doing anything with the method. But, hey NCover did make you to include the methods. That is the first step in become Agile Developer.