如何使用多个接口模拟受保护的成员

时间:2014-01-13 13:45:04

标签: c# asp.net-mvc unit-testing moq

如何在nUnit测试中模拟这个类?

public class OpenDataQuery: PagedQuery, IOpenDataQuery
    {
        private static Dictionary<string, SortItem> m_sortModes;

        protected override Dictionary<string, SortItem> SortModes
        {
            get
            {
                if (m_sortModes == null)
                {
                    m_sortModes = new Dictionary<string, SortItem>();

                    AddSortMode(m_sortModes, new SortItem(ObjectExtensions.GetNameFromExpression<OpenDataCategoriesModel, string>(m => m.Name), "Наименование ↑", true) { IsDefault = true });
                    AddSortMode(m_sortModes, new SortItem(ObjectExtensions.GetNameFromExpression<OpenDataCategoriesModel, string>(m => m.Name), "Наименование ↓"));
                }

                return m_sortModes;
            }
        }

        public IEnumerable<OpenDataCategoriesModel> OpenDataCategories { get; set; }

        public string OpenDataTags { get; set; }
    }

public abstract class PagedQuery : IPagedQuery
    {
        private const int DEFAULT_PAGE = 1;
        private const int DEFAULT_COUNT = 5;

        private int? m_page;
        private int? m_count;
        private int? m_total;
        private string m_sort;

        public int? Page
        {
            get
            {
                if (m_page == null || m_page <= 0)
                {
                    return DEFAULT_PAGE;
                }

                return m_page;
            }
            set { m_page = value; }
        }

        public int? Count
        {
            get
            {
                if (m_count == null || m_count <= 0)
                {
                    return DEFAULT_COUNT;
                }

                return m_count;
            }
            set { m_count = value; }
        }

        public int? Total
        {
            get
            {
                if (m_total == null || m_total <= 0)
                {
                    return 0;
                }

                return m_total;
            }
            set { m_total = value; }
        }

        public string SearchQuery { get; set; }

        protected virtual Dictionary<string, SortItem> SortModes
        {
            get { return null; }
        }

        public string Sort
        {
            get
            {
                var sortMode = GetSortMode(m_sort);

                if (sortMode == null)
                {
                    var defaultSort = (from i in SortModes where i.Value.IsDefault select i).FirstOrDefault();

                    if (!string.IsNullOrWhiteSpace(defaultSort.Key))
                    {
                        return defaultSort.Key;
                    }

                    return (from i in SortModes select i.Key).First();
                }

                return m_sort;
            }
            set
            {
                m_sort = value;
            }
        }

        protected void AddSortMode(Dictionary<string, SortItem> sortModes, SortItem sortItem)
        {
            sortModes.Add(
                String.Format(
                    "{0}{1}",
                    sortItem.FieldName.ToLower(),
                    sortItem.Asc ? "asc" : "desc"
                ),
                sortItem
            );
        }

        private SortItem GetSortMode(string sort)
        {
            if (SortModes == null || string.IsNullOrWhiteSpace(sort) ||
                !SortModes.ContainsKey(sort.ToLower()))
            {
                return null;
            }
            return SortModes[sort.ToLower()];
        }

        public IOrderBy GetOrderBy()
        {
            var item = GetCurrentSortItem();

            if (item == null)
            {
                return null;
            }

            if (item.Asc)
            {
                return new OrderBy(item.FieldName);
            }

            return new OrderByDesc(item.FieldName);
        }

        public SortItem GetCurrentSortItem()
        {
            return GetSortMode(Sort);
        }

        public Dictionary<string, SortItem> GetSortItems()
        {
            return SortModes;
        }
    }

public interface IOpenDataQuery : IPagedQuery
    {
        string OpenDataTags { get; set; }

    }

我有一些服务方法,在参数和单元测试中使用openDataQuery类我正在尝试模拟这个类,但这不起作用:

    public partial class OpenDataQueryRepository : Mock<OpenDataQuery>
        {
            public OpenDataQueryRepository(MockBehavior mockBehavior = MockBehavior.Strict)
                : base(mockBehavior)
            {
                          var opendataQuery = new Mock<IOpenDataQuery>();

                          var pagedQuery = opendataQuery.As<IPagedQuery>();

                          this.Setup(p=>p.GetOpenDataCategoriesMain(pagedQuery.Object,outttl)).Returns(OpenDataCategories);


            }
        }

我知道我应该将Moq.Protected()用于受保护的方法,但我不知道在这种情况下如何正确使用它。请帮帮我。

更新 我正在测试这个控制器:

public class ODOpenDataController : ODBaseController
{
        private readonly IOpenDataProvider m_openDataProvider;

        public ODOpenDataController(IOpenDataProvider openDataProvider)
        {
            m_openDataProvider = openDataProvider; 
        }    

        public ActionResult Index(OpenDataQuery query)
        {
            int total;    

            query.OpenDataCategories = m_openDataProvider.GetOpenDataCategoriesMain(query, out total)

            query.Total = total;
            return View(query);
        }

}

测试:

[Test]
public void Index_Test()
{
    var opendataController = new ODOpenDataController(new OpenDataRepository().Object);

    var result = opendataController.Index(new OpenDataQuery()) as ViewResult;
    var model = result.Model as OpenDataQuery;

    Assert.IsTrue(model.OpenDataCategories.Count() == 1);
}

0 个答案:

没有答案