/moq_datareader

Library helps in mocking IDataReader using Moq for unit testing

Primary LanguageC#MIT LicenseMIT

A simple Moq extensions library that helps in mocking IDataReader

Kindly raise any issues at Project Issues

Build PivotalServices.MoqExtensions.DataReader
Nuget (Prod Release) NuGet

Salient features

  • Works with Moq
  • Provides extensions to mock IDataReader using Moq, especially when unit testing Repositories
  • Supports most of the data types (string, int, short, long, float, decimal, double, guid, datetime, bool, char and byte)

Package

Usage Instructions

    using PivotalServices.MoqExtensions.DataReader;
    var mockDataTable = new MockDataTable();

    //Add Columns (schema)
    mockDataTable.Columns.Add(new MockDataColumn("Column1", typeof(string)));
    mockDataTable.Columns.Add(new MockDataColumn("Column2", typeof(string)));
    mockDataTable.Columns.Add(new MockDataColumn("Column3", typeof(string), true));

    //Add Rows (data) - column order is important here
    mockDataTable.Rows.Add(new MockDataRow("Row1_Column1_Value", "Row1_Column2_Value", "Row1_Column3_Value"));
    mockDataTable.Rows.Add(new MockDataRow("Row2_Column1_Value", "Row2_Column2_Value", "Row2_Column3_Value"));
    mockDataTable.Rows.Add(new MockDataRow("Row3_Column1_Value", "Row3_Column2_Value", "Row3_Column3_Value"));
    mockDataTable.Rows.Add(new MockDataRow("Row4_Column1_Value", "Row4_Column2_Value", "Row4_Column3_Value"));
  • Create a mock datareader as below using Moq and setup using the extension method SetupWithReturn
    var reader = new Mock<IDataReader>();

    //Setup reader to return the data from datatable
    reader.SetupWithReturn(mockDataTable);
  • You should be all set to use your mocked reader for unit testing a Repository or any class that uses IDataReader. Sample implementation given below.
    while (reader.Read())
    {
        models.Add(new Model
        {
            Column1 = reader.GetString(reader.GetOrdinal("Column1")),
            Column2 = reader.GetString(reader.GetOrdinal("Column2")),
            Column3 = Convert.IsDBNull(reader.GetOrdinal("Column3")) ? null : reader.GetString(reader.GetOrdinal("Column3")),
        });
    }   

Issues

Contributions are welcome!