如何模拟ExecuteReader方法?

时间:2016-08-17 09:34:56

标签: .net unit-testing nunit idatareader stubs

我是moq的新手并设置了嘲讽,所以我可以提供一些帮助。如何使用ExecuteReader()模拟Moq

更新

我有以下ExecuteReader代码

    private void ExecuteGainLossCommand(AccountsContext dbContext, string query, MIS_GainLoss collGainLoss)
    {
        try
        {
            var cmd = dbContext.Database.Connection.CreateCommand();
            if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open();
            cmd.CommandText = query;
            var reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                collGainLoss.collGainLoss = ((IObjectContextAdapter)dbContext)
                    .ObjectContext
                    .Translate<MIS_GainLossHolding>(reader).ToList<MIS_GainLossHolding>();
            }
            //Fetch Total's result set                          
            reader.NextResult();

            reader.NextResult();
            if (reader.HasRows)
            {
                collGainLoss.collPurchaseData = ((IObjectContextAdapter)dbContext)
                            .ObjectContext
                            .Translate<MIS_GainLossHolding>(reader).ToList<MIS_GainLossHolding>();
            }
            reader.Close();
        }
        finally 
        {
            dbContext.Database.Connection.Close();
        }
    }

这是一个模型

    public class MIS_GainLoss
      {
          public ICollection<MIS_GainLossHolding> collGainLoss { get; set; }
          public ICollection<MIS_GainLossHolding> collPurchaseData = new List<MIS_GainLossHolding>();        
      }   



   public class MIS_GainLossHolding
     {         
        public Guid Id { get; set; } 
        public string Name { get; set; }
        public string Symbol { get; set; } 
     }

以下是我想在单元测试中模拟的模拟数据

      var holdings =
            new List<MIS_GainLoss>{
             new MIS_GainLoss
                {
                    collGainLoss = new[]
                        {
                            new MIS_GainLossHolding { 
                                Id = Guid.NewGuid(),
                                Name = "ASTON/River Road Dividend All Cap Val N",
                                Symbol = "ARDEX",

                            },
                                new MIS_GainLossHolding { 
                                Id = Guid.NewGuid(),
                                Name = "Champlain Mid Cap Adv",
                                Symbol = "CIPMX",

                            }
                        },
                    collPurchaseData = new[]
                        {
                            new MIS_GainLossHolding { 
                                Id = Guid.NewGuid(),
                                Name = "ASTON/River Road Dividend All Cap Val N",
                                Symbol = "ARDEX",

                            },
                                new MIS_GainLossHolding { 
                                Id = Guid.NewGuid(),
                                Name = "Champlain Mid Cap Adv",
                                Symbol = "CIPMX",

                            }
                        },
                }
    };

一些人建议 IDataReader apporoch适合它,我该如何实现? 如何为此代码编写单元测试用例

0 个答案:

没有答案