If you want to mock IDataReader to return a list of records, you can create a class that implements IDataReader and overrides some of its methods (for example, Read () and Indexer). In addition, you will need a variable to save a record of the current line and a variable to store the list values. The following is sample code for this:
public class MockDataReader : IDataReader { private int _rowCounter = 0; private List<Dictionary<string,object>> _records = new List<Dictionary<string,object>>(); public MockDataReader(List<Dictionary<string,object>> records) { _records = records; } public bool Read() { _rowCounter++; if (_rowCounter < _records.Count) return true; return false; } public object this[string name] { get { return _records[_rowCounter][name]; } } }
Then, to use this class, you can use the following code:
var itemsList = new List<Dictionary<string, object>>(); for (int i = 0; i < 5; i++) { var num = i + 1; var items = new Dictionary<string, object>(); items.Add("Id", num); items.Add("FirstName", "MyFirstName" + num); items.Add("LastName", "MyLastName" + num); itemsList.Add(items); } var result = new MockDataReader(itemsList);
Not full proof, but works. Hope this helps :)
param3216
source share