存储库模式中的单元测试c#

时间:2017-07-31 23:03:52

标签: c# unit-testing repository-pattern

我刚刚开始学习Repository Pattern与接口层,DAL层,BLL层分离。我想过将单元测试写入我的存储库。有什么方法可以测试我的存储库返回项目列表。 这是调用逻辑方法的aspx页面。

public static object GetActivityList()
    {
        bool? active = null;
        active = String.IsNullOrWhiteSpace(HttpContext.Current.Request.Params["status"]) ? active : Convert.ToBoolean(Convert.ToInt16(HttpContext.Current.Request.Params["status"]));
        var search=String.IsNullOrWhiteSpace(HttpContext.Current.Request.Params["search"].ToString())?String.Empty:HttpContext.Current.Request.Params["search"].ToString();
        using (var activityLogic = dependencyContainer.Resolve<IActivityLogic>())
        {
            List<ActivityDTO> activityList = activityLogic.GetActivityList(AccountId, active);
}
}

然后我的实体类为:

public class ActivityDTO
{
    public string ActivityCode { get; set; }
    public string ActivityName { get; set; }
    public bool Billable { get; set; }
    public bool Payable { get; set; }
    public string PayCode { get; set; }
    public string DeptCode { get; set; }
    public bool Active { get; set; }
    public bool IsDefault { get; set; }
}

我的界面为:

 public interface IActivityLogic:IDisposable
    {
        List<ActivityDTO> GetActivityList(long accountId, bool? active);
    }

这是我的Businesslogic类:

 public class ActivityLogic:IActivityLogic
    {
        private IActivityRepository data;

        public ActivityLogic()
        {
            var dependencyContainer = new UnityContainer().LoadConfiguration();
            this.data = dependencyContainer.Resolve<IActivityRepository>();
        }
        public void Dispose()
        {
            this.data.Dispose();
            GC.SuppressFinalize(this);
        }
        public List<ActivityDTO> GetActivityList(long accountId, bool? active)
        {
            return this.data.GetActivityList(accountId, active);
        }
    }

这是我的DAL界面:

 public interface IActivityRepository:IDisposable
    {
        List<commonEntities.ActivityDTO> GetActivityList(long accountId, bool? active);
    }

这是我的数据存储库:

 public List<commonEntities.ActivityDTO>GetActivityList(long accountId, bool? active)
        {
            var query = from a in dataContext.Activities
                        where a.AccountID == accountId
                        select new commonEntities.ActivityDTO
                        {
                            ActivityCode = a.ActivityCode,
                            ActivityName = a.ActivityName,
                            Billable = a.Billable,
                            Payable = a.Payable,
                            PayCode = a.PayCode,
                            DeptCode = a.DeptCode,
                            Active = a.Active,
                            IsDefault = a.IsDefault
                        };
            if (active != null)
            {
                query = query.Where(r => r.Active==active);
            }
            return query.ToList();
        }

我想为这个回购编写单元测试。我可以使用任何测试策略,或者我如何对这种逻辑进行单元测试。

0 个答案:

没有答案