|

Creating C# Class Properties And Tests

Creating C# Classes And Tests

In this tutorial, we are going to start the beginnings of a fictional CRM type application. A customer relationship management system simply keeps track of customers and their related data. We’ll use Visual Studio to build out a new Customer class, then define the properties that we need for a customer. These will include things like the first name, last name, email address, and so on. Along the way, we’ll add a test project to our visual studio solution. In that project, we’ll create an accompanying test class for the Customer class to ensure that it works properly. Let’s get started.


Adding A New Project

To get started building our new Customer class we first need a new project in Visual Studio. We can click on File->New->Project to bring up the new class wizard.
visual studio file new project

Once there we can choose a name for the solution as well as the project. The project name is CRMBIZ and the solution is simply CRM. CRMBIZ stands for “Customer Relationship Manager Business Layer” and of course CRM simply stands for “Customer Relationship Manager”.
visual studio solution name

After all of this is taken care of, we are given a new class file to start with in the editor and solution explorer.
new class created


Building A Customer Class

Let’s rename that default class to Customer.cs and build out the class like we see below. Our goal here is to create a Class that can represent a customer in our CRM system. We can see the following members of the class.

  • lastName
  • LastName
  • FirstName
  • EmailAddress
  • CustomerId
  • FullName


Build A Unit Test For The Class

We want to ensure our class is working by using a unit test. It makes sense to create a new folder to hold the unit test project. So we go ahead and right-click on the solution name, then choose Add->New Solution Folder and name it Test.
visual studio add new solution folder

Once that is complete we can add a new test project into the new solution folder. Right click on the Test folder, then choose Add->New Project, and choose the Unit Test option.
add new project to solution visual studio

In our case we name it CRMBIZ.Test. You can name yours whatever you like. In order for the test project to actually be able to test the code in our main project, we need to reference an assembly from the main project. This can be done by right clicking on the References node of the test project, then choosing to add the reference to the main project like we see here.

add reference to project you want to test


Adding Test Methods

In our new test project, we can rename the default test file to CustomerTest.cs. Our goal is to test the Customer class in the project, so the convention is to use the name of the class followed by the word Test. Testing follows the same approach for any situation.

  • Set up the data
  • Take an action
  • Assert the results

So that is what we do below. First we set up the data by creating a new Customer object. This is an actual object derived from the class we created above.


CustomerTest.cs

What does the code above do for us? Let’s examine. First, we create a new customer object.
create new customer object


Assign data into the FirstName property.
assign data to firstname property


Assign data into the LastName property.
assign data to lastname property


Next we set up the expected data we want to see after we access the customer.FullName property. This should return to us the last name, a comma, then a space, and finally the first name of the customer.
the expected result


We are then ready to take an action. We access the FullName property and store the result in the actual variable.
take an action


Finally, we can assert if this condition is true.
c sharp assert-areequal


When we run the test in test explorer, it looks like it is working!
test explorer run all tests


Beefing Up The FullName Getter

We can modify the getter in the Customer class for FullName now. With the updated code below, some logic is added to determine whether or not a comma is needed depending on if you have the first name only, or the last name only.

Now we can better test the class. We can check to see that all the properties are working as expected.

Running all tests shows that the class appears to be working great.
tests are still passing


Objects Are Reference Types

We saw in an earlier tutorial that in C#, objects are reference types. Quickly recapping, reference types store a pointer to their data. Value types store their data directly. This is an important concept. Essentially, you can have multiple variables that point to the same data when using reference types. We prove this out

Below we make use of the static modifier on a new member of Customer, InstanceCount. The static modifier declares that a member belongs directly to the class, rather than to an object instantiated from the class. Therefore you access a static member using the class name, not the object variable.


Testing Static Members

Let’s test the concept above out. In the updated code below we now have a test method named StaticTest(). The test creates three new customer objects, and stores them in three different variables. Each variable references a different customer object. Notice what we are doing after each assignment of a name to the FirstName property in each customer object. We are making use of that static InstanceCount from above to increment its value each time a new object is created and assigned a value. We then assert that the reference count is three like we believe it should be.

Running the suite of tests shows that things are still working quite nicely!
static instanceof class test


Creating C# Class Properties And Tests Summary

In this tutorial we used Visual Studio to create a new solution and project to get started creating a Customer class for a CRM type application. Next up we built the Customer class, which as we learned before, defines a type. Anytime you use the class keyword in C#, you are creating a type. By default, a new class added to a project in Visual Studio have no access modifier. This means that if you want to access the class from other components, you’ll need to make an adjustment and add the public keyword to the class. We also got a feeling for test driven development in C#, as we created an accompanying test class to the Customer class named CustomerTest.