Как Shim ExecuteReader с MS Fake

Я пытаюсь проверить метод ниже, но я получаю сообщение об ошибке using (var reader = command.ExecuteReader()) это соединение должно быть открыто и доступно. Я не могу скинуть этот метод и вернуть поддельного читателя.

Метод для тестирования

 public EmailTemplateFilterModel[] GetEmailTemplateFilters()
        {
            var filterList = new List<EmailTemplateFilterModel>();

            using (var connection = new SqlConnection(_clientConnString))
            using (var command = connection.CreateCommand("cspGetInvoiceTemplateFilterCollections"))
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var filter = new EmailTemplateFilterModel
                        {
                            ParmId = (int)reader["InvoiceEmailMapParmId"],
                            ParmName = (string)reader["ParmName"],
                            DatabaseDataType = (string)reader["DbDataType"],
                            ControlType = (string)reader["ControlType"],
                            StoredValue = (string)reader["StoredValue"],
                            DisplayedValue = (string)reader["DisplayedValue"]
                        };

                        filterList.Add(filter);
                    }
                }
            }
            return filterList.ToArray();
        }

Модульный тест

 [Test()]
        public void GetEmailTemplateFiltersTest()
        {
            using (ShimsContext.Create())
            {
                var rowCounter = 0;

                //ARRANGE
                ShimSqlConnection.AllInstances.Open = (sender) => { };
                ShimSqlConnection.AllInstances.StateGet = (sender) => { return ConnectionState.Open; };
                //ShimSqlCommand.AllInstances.ExecuteReader = (sender) => new Mock<SqlDataReader>().Object;

                ShimSqlDataReader.ConstructorSqlCommandCommandBehavior = (@this, p1, p2) =>
                {
                    var shim = new ShimSqlDataReader(@this)
                    {

                        // ValueGet = () => -5
                    };
                };


                ShimSqlDataReader.AllInstances.Read = (sender) =>
                {
                    rowCounter++;
                    return rowCounter <= 2;
                };

                ShimSqlDataReader.AllInstances.ItemGetString = (sender, coloumnName) =>
                {
                    if (rowCounter == 1)
                    {
                        switch (coloumnName)
                        {
                            case "InvoiceEmailMapParmId":
                                return 1;
                            case "ParmName":
                                return "Param1";
                            default:
                                return "SomeStrigValue";
                        }
                    }
                    else if (rowCounter == 2)
                    {
                        switch (coloumnName)
                        {
                            case "InvoiceEmailMapParmId":
                                return 1;
                            case "ParmName":
                                return "Param1";
                            default:
                                return "SomeStrigValue";
                        }
                    }
                    else
                    {
                        return string.Empty;
                    }
                };

                //ACT
                var filterList = _sut.GetEmailTemplateFilters();

                //ASSERT
                Assert.That(filterList.Length, Is.EqualTo(rowCounter));

            }
        }

1 ответ

  [Test()]
        public void GetEmailTemplateFiltersTest()
        {
            using (ShimsContext.Create())
            {
                #region ARRANGE
                var rowCounter = 0;

                ShimSqlConnection.AllInstances.Open = (sender) => { };
                ShimSqlConnection.AllInstances.StateGet = (sender) => ConnectionState.Open;
                ShimSqlCommand.AllInstances.ExecuteReader = (sender) => new ShimSqlDataReader();
                ShimDbDataReader.AllInstances.Dispose = (sender) => { };
                ShimDbDataReader.AllInstances.DisposeBoolean = (sender, p1) => { };

                ShimSqlDataReader.AllInstances.Read = (sender) =>
                {
                    rowCounter++;
                    return rowCounter <= 2;
                };

                ShimSqlDataReader.AllInstances.ItemGetString = (sender, coloumnName) =>
                {
                    if (rowCounter == 1)
                    {
                        switch (coloumnName)
                        {
                            case "InvoiceEmailMapParmId":
                                return 1;
                            case "ParmName":
                                return "Param1";
                            default:
                                return "SomeStrigValue";
                        }
                    }
                    else if (rowCounter == 2)
                    {
                        switch (coloumnName)
                        {
                            case "InvoiceEmailMapParmId":
                                return 1;
                            case "ParmName":
                                return "Param1";
                            default:
                                return "SomeStrigValue";
                        }
                    }
                    else
                    {
                        return string.Empty;
                    }
                }; 
                #endregion

                //ACT
                var filterList = _sut.GetEmailTemplateFilters();

                //ASSERT
                Assert.That(filterList.Length, Is.EqualTo(2));

            }
        }
Другие вопросы по тегам