代码之家  ›  专栏  ›  技术社区  ›  netmajor

在WCF服务上测试DataAccess类生成

  •  0
  • netmajor  · 技术社区  · 14 年前

    我将我的NHibernate数据访问类放在WCF服务中,以便Silverlight项目可以使用它,但我有错误,希望测试我的查询。

    可以使用NUnit在服务类中测试这个查询吗?以前我通常测试这个类,但是当它在服务类时怎么做??

    这是我的WCF服务类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    using DataTransfer;
    using NHibernate;
    using NHibernate.Cfg;
    using System.Diagnostics;
    
    namespace WcfService1
    {
        public class Service1 : IService1
        {
            private ISession _session;
            public Service1()
            {
                try
                {
                    _session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
                }
                catch (Exception e)
                {
                    Debug.Write(e);
                    throw;
                }
            }
            public IList<Dziecko> GetChildByFirstname(string _firstname)
            {
                return _session.CreateCriteria(typeof(Dziecko))
                    .Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).List<Dziecko>();
            }
            public IList<Dziecko> GetChildByLastname(string _lastname)
            {
                return _session.CreateCriteria(typeof(Dziecko))
                    .Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
            }
            public IList<Dziecko> GetChildByFirstnameAndLastname(string _firstname, string _lastname)
            {
                return _session.CreateCriteria(typeof(Dziecko))
                    .Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
            }
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   tinonetic    8 年前

    如果您想测试查询本身,我建议将它们放入一个单独的程序集中(可能使用 repository pattern )并从服务调用此程序集中的方法。这将使测试查询本身更容易,还允许您在测试服务时模拟存储库。