0

我是新手moq并设置了模拟,所以我可以在一点帮助下做。如何模拟ExecuteReader()using 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 是否适合它,我该如何实现?我如何为这段代码编写单元测试用例

4

0 回答 0