I'm new to Unit Tests so I've been trying to code some examples to learn the right way to use them. I have an example project which uses Entity Framework to connect to a database.
I'm using an n-tier architecture composed by a data access layer which queries the database using EF, a business layer which invokes data access layer methods to query database and perform its business purpose with the data retrieved and a service layer which is composed of WCF services that simply invoke business layer objects.
Do I have to code unit tests for every single layer (data access, business layer, services layer?
Which would be the right way to code a unit test for a method that queries a database? The next code is an example of a method in my data access layer which performs a select on the database, how should its unit test be like?
public class DLEmployee
{
private string _strErrorMessage = string.Empty;
private bool _blnResult = true;
public string strErrorMessage
{
get
{
return _strErrorMessage;
}
}
public bool blnResult
{
get
{
return _blnResult;
}
}
public Employee GetEmployee(int pintId)
{
Employee employee = null;
_blnResult = true;
_strErrorMessage = string.Empty;
try
{
using (var context = new AdventureWorks2012Entities())
{
employee = context.Employees.Where(e => e.BusinessEntityID == pintId).FirstOrDefault();
}
}
catch (Exception ex)
{
_strErrorMessage = ex.Message;
_blnResult = false;
}
return employee;
}
N-tier application architecture provides a model by which developers can create flexible and reusable applications. By segregating an application into tiers, developers acquire the option of modifying or adding a specific tier, instead of reworking the entire application.
N-tiered refers to the "distributed" layers of a system (i.e. server and client), whereas n-layered refers to the layers in a self-contained program; although the two are often used interchangeably, some suggest that there is a significant difference (like the one I mentioned above), as seen on the first paragraphs on ...
In 3 Tier Application there are three tiers like Presentation Layer , Application Layer and Data layer. Here the application layer contains business logic as well . On the other hand in N Tier Application layer is divided into 2 i.e. Application Layer and Business Logic layer.
Unit testing is the first testing phase and it is practiced before moving to the phase of integration testing. Hence, before moving for the next testing level, make sure to fix all the identified bugs in the unit testing phase.
Here are my 2 cents based on Domain Driven Design principles:
If you just started on unit testing, I advise you to read Kent Beck's Test Driven Development by Example and Gerard Meszaros' xUnit Test Patterns
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With