views:

316

answers:

1

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.

+1  A: 

Instead of trying to explain this myself, the best advice I can give you is learning about and using the S#arp architecture, which helps you with setting up the nUnit tests, the DAO and the DAL layer and much more. It also enforces using NHibernates Best Practices and more. You can read more about the S#arp architecture and how to set it up and run nUnit tests etc, on Bill McCafferty's blog (also the one maintaining the S#arp libs).

Note: for getting started quickly, the S#arp libs come with a plethora of examples to show you how it's done. And if S#arp seems like too much, you can follow the link on NH Best Practices above, it has quite some notes on how to go about setting up nUnit tests in a unified and generic way.

Abel
+1 very very interesting.
SnOrfus