Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have a data layer which returns List<user> where "User" is a class object. What is the correct way to write Nunit test method for this. I am new to Nunit, I attempted to write using Assert calls. This looks insufficient.
Iam unsure how I have to handle Nunit here. Any help appreciated.
Many thanks!


private string s_connection = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FormHelperClass.DataBaseName + ";Jet OLEDB:System Database = " + FormHelperClass.SystemDataBasePath + ";User Id=userid;Password=password"; 
public List<User> GetAllUsers()
{
	List<User> users = new List<User>();
	try
	{
		//query the database 
		string query = "SELECT UserID, UserName FROM User order by UserId;";
		// setup the connection
		OleDbConnection connection = new OleDbConnection(s_connection);
		connection.Open();
		OleDbCommand command = new OleDbCommand(query, connection);
		OleDbDataReader reader = command.ExecuteReader();
		// loop through reader & read configs 
		while (reader.Read())
		{
			User user = new User();
			user.UserId = Convert.ToInt16(reader["UserID"]);
			user.UserName = (reader["UserName"]).ToString();                    
			users.Add(user);
		}
		reader.Close();
		connection.Close();
	}
	catch (Exception ex)
	{                
		log.Debug("Error Message " + ex.Message);
	}
	return users;
}
using NUnit.Framework;
    [TestFixture]
    public class ServiceProvidersTest
    {
        [Test]
        public void TestGetUsers()
        {
            FormHelperClass.DataBaseName = "C:\\Users\\Security.mdb";
            UserManager u_manager = new UserManager();
            List<User> list_manager = u_manager.GetAllUsers();
            
            Assert.IsNotNull(list_manager);
            Assert.Greater(list_manager.Count, 0); 
            foreach(User user in list_manager)
            {
                Assert.IsNull(user);
               
                
            }
        }
    }
Posted
Updated 6-Oct-11 14:34pm
v2

There are different ways you can perform unit testing of DAL.

Option 1: In-memory database
Using an in-memory database and for testing run all the tests agains it. It is best if you can choose an in-memory database with which offers almost similar syntax as the actual database you are using.

Option 2: Mocking the ADO.Net
OleDbConnection has an underlying interface IDbConnection
OleDbCommand has IDbCommand and so on.

You can use DI, automatic using containers or manual or a factory implementation would also do, to inject the right dependency depending at runtime.

Option 2 could be an expensive choice, I suggest take it only if your DAL has to resolve complex hierarch / dependencies.

Option 3:
Follow the link
Unit Testing the Data Access Layer[^]

Unit Testing the Data Access Layer 2[^] see if this suites your need.
 
Share this answer
 
v2
There is no correct way of writing test and most of the time efficiency is of no concern as the tool is doing all the work.

You must note that writing great tests is not the goal, but the goal is writing great software so the test are only there to support the main code.

Principals to follow in writing test:

1) They should be repeatable, so you must have setup and cleanup code before and after.
2) Be as granular as you need to be, its better to test a specific feature than to test everything at once.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900