【问题标题】:How to test Nhibernate with Nunit?如何使用 Nunit 测试 Nhibernate?
【发布时间】:2010-12-13 07:00:56
【问题描述】:

抱歉,重新发布。

我正在为 ORM 使用 Nhibernate,并且有这个类我需要使用 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;
        }


    }
}

我从构造函数开始,但有几个人建议我它不是真的必要。所以这是我需要测试的方法。它查询数据库并给我一个成分对象列表。我很难开始测试 getIngredientList() 方法。我有这个测试存根:

[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);

        }

我还有很多其他类似的方法需要测试,所以如果有人可以帮助我开始这个,我将对如何在我的其他方法上实现单元测试有一个基本的了解。

再次感谢您的宝贵时间和建议。

【问题讨论】:

    标签: c# unit-testing nhibernate nunit


    【解决方案1】:

    与其试图自己解释这一点,我能给您的最佳建议是了解和使用S#arp architecture,它可以帮助您设置 nUnit 测试、DAO 和 DAL 层等等。它还强制使用NHibernates Best Practices 等。您可以在Bill McCafferty's blog(也是维护 S#arp 库的网站)上阅读有关 S#arp 架构以及如何设置和运行 nUnit 测试等的更多信息。

    注意:为了快速入门,S#arp 库提供了大量示例来向您展示它是如何完成的。如果 S#arp 看起来太多了,你可以点击上面 NH Best Practices 的链接,它有很多关于如何以统一和通用的方式设置 nUnit 测试的说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      相关资源
      最近更新 更多