/DataMocks

This library contains mocks for System.Data classes (Currently IDataReader supported)

Primary LanguageC#

DataMocks

Build status Test results Nuget

Information

This library contains mocks for System.Data classes (Currently IDataReader is supported)

If you are developing some IDataReader related tools (e.g., IDataReader extensions), this library suits you perfectly.

Installation

You can install DataMocks by copying and pasting the following command into your Package Manager Console within Visual Studio (Tools > NuGet Package Manager > Package Manager Console).

Install-Package DataMocks

Examples

We have two MockDataReaderBuilder for now

1) SimpleMockDataReaderBuilder

You can create data reader with manually adding columns and the data

[TestMethod]
public void CanReadAllDataProperly()
{
    var builder = new SimpleMockDataReaderBuilder().
        SetNullValueHandling(NullValueHandling.AssumeAsDbNull).
        AddColumn("ID_PRODUCT", typeof(long)).
        AddColumn("PRODUCT_NAME", typeof(string)).
        AddColumn("IS_ACTIVE", typeof(bool)).
        AddColumn("BARCODE", typeof(string)).
        AddData(1L, "Product 1", true, null);

    using (var reader = new MockDataReader(builder))
    {
        reader.Read().ShouldBe<bool>(true);
        reader.IsDBNull(0).ShouldBe<bool>(false);
        reader.GetString(1).ShouldBe<string>("Product 1");
        reader["IS_ACTIVE"].ShouldBe<object>(true);
        reader["BARCODE"].ShouldBe<object>(DBNull.Value);
    }
}

2) ObjectBasedMockDataReaderBuilder

You can create MockDataReader with List<TEntity>

private class TestObjectClass
{
    public int IntProperty { get; set; }
    public string StringProperty { get; set; }
}
[TestMethod]
public void CanReadAllDataProperly()
{
    var dataList = new List<TestObjectClass>();
    dataList.Add(new TestObjectClass { IntProperty = 10, StringProperty = "ten" });
    dataList.Add(new TestObjectClass { IntProperty = 20, StringProperty = "twenty" });
    dataList.Add(new TestObjectClass { IntProperty = 30, StringProperty = "thirty" });

    var builder = new ObjectBasedMockDataReaderBuilder<TestObjectClass>().
        AddData(dataList);

    using (var reader = new MockDataReader(builder))
    {
        reader.Read(); // Seek to the first item
        reader.Read(); // Seek to the second item

        reader.GetInt32(0).ShouldBe<int>(20);
        reader.GetString(1).ShouldBe<string>("twenty");
    }
}