Sorry for the repost.
I'm using Nhibernate for ORM and have this class I need to perform unit tests using Nunit:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NutritionLibrary.Entity;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace NutritionLibrary.DAO
{
public class IngredientDAONHibernate : NutritionLibrary.DAO.IngredientDAO
{
private Configuration config;
private ISessionFactory factory;
public IngredientDAONHibernate()
{
config = new Configuration();
config.AddClass(typeof(NutritionLibrary.Entity.Ingredient));
config.AddClass(typeof(Entity.Nutrient));
config.AddClass(typeof(Entity.NutrientIngredient));
factory = config.BuildSessionFactory();
}
/// <summary>
/// gets the list of ingredients from the db
/// </summary>
/// <returns>IList of ingredients</returns>
public System.Collections.Generic.IList<Ingredient> GetIngredientList()
{
System.Collections.Generic.IList<Ingredient> ingredients;
string hql = "from NutritionLibrary.Entity.Ingredient ingredient";
ISession session = null;
ITransaction tx = null;
try
{
session = factory.OpenSession();
tx = session.BeginTransaction();
IQuery q = session.CreateQuery(hql);
ingredients = q.List<Ingredient>();
tx.Commit();
}
catch (Exception e)
{
if (tx != null) tx.Rollback();
/*if (logger.IsErrorEnabled)
{
logger.Error("EXCEPTION OCCURRED", e);
}*/
ingredients = null;
}
finally
{
session.Close();
session = null;
tx = null;
}
return ingredients;
}
}
}
I started with the constructor, but I got a few people advising me its not really necessary. So this is my method that I need to test. It queries the db and gives me a List of ingredient objects. I am having difficulty getting started with how to test the getIngredientList() method. I have this test stub:
[TestMethod()]
public void GetIngredientListTest()
{
IngredientDAONHibernate target = new IngredientDAONHibernate(); // TODO: Initialize to an appropriate value
IList<Ingredient> expected = null; // TODO: Initialize to an appropriate value
IList<Ingredient> actual;
actual = target.GetIngredientList();
Assert.AreEqual(expected, actual);
}
I have a lot of other similar methods that I have to test, so if someone could be kind enough to help me get started with this, I will have a basic idea of how to implement unit tests on my other methods.
Once again, thank you for your time and advise.