Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# NUnit
Hi,
 
I have a data layer which returns List 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 6-Oct-11 14:32pm
sujanir749
Edited 6-Oct-11 14:34pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 7 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100