【发布时间】: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