2009-11-05 2 views
1

죄송합니다.Nunit을 사용하여 Nhibernate를 테스트하는 방법은 무엇입니까?

내가 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; 
     } 


    } 
} 

내가 생성자로 시작을하지만, 나는 그것 정말 필요하지 말라고 조언 몇 사람을 얻었다. 그래서 이것은 시험 할 필요가있는 나의 방법입니다. 그것은 db를 쿼리하고 나에게 성분 객체 목록을 제공합니다. 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); 

     } 

내가 테스트 할 수있는 다른 유사한 방법을 많이 가지고가, 그래서 누군가가 나이 시작하는 데 도움이 친절하게도이 될 수 있다면, 나는 방법에 대한 기본적인 생각을해야합니다 내 다른 방법에 대한 단위 테스트를 구현합니다.

다시 한 번 시간을내어 알려 주시고 조언 해주십시오.

답변

3

이 설명을하는 대신 가장 좋은 조언은 S#arp architecture을 배우고 사용하는 것입니다.이 도구는 nUnit 테스트, DAO 및 DAL 레이어 설정에 도움이됩니다. 또한 NHibernates Best Practices 이상을 사용하여 시행합니다. S # arp 아키텍처와 그 설정 방법 및 nUnit 테스트 수행 방법에 대한 자세한 내용은 Bill McCafferty's blog (S # arp 라이브러리를 유지 관리하는 것도 포함)에서 읽을 수 있습니다.

참고 : 빠른 시작을 위해 S # arp 라이브러리에는 많은 작업이 포함되어 있습니다. S # arp가 너무 많아 보인다면 위의 NH Best Practices 링크를 따라갈 수 있습니다. nUnit 테스트를 통일되고 일반적인 방식으로 설정하는 방법에 대한 노트가 있습니다.

+0

+1 매우 흥미 롭습니다. –

관련 문제