Click here to Skip to main content
13,252,815 members (51,353 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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>();
		//query the database 
		string query = "SELECT UserID, UserName FROM User order by UserId;";
		// setup the connection
		OleDbConnection connection = new OleDbConnection(s_connection);
		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();                    
	catch (Exception ex)
		log.Debug("Error Message " + ex.Message);
	return users;
using NUnit.Framework;
    public class ServiceProvidersTest
        public void TestGetUsers()
            FormHelperClass.DataBaseName = "C:\\Users\\Security.mdb";
            UserManager u_manager = new UserManager();
            List<User> list_manager = u_manager.GetAllUsers();
            Assert.Greater(list_manager.Count, 0); 
            foreach(User user in list_manager)
Posted 6-Oct-11 15:32pm
Updated 6-Oct-11 15:34pm
Rate this: bad
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.
Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 7 Oct 2011
Copyright © CodeProject, 1999-2017
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