How to wave DataReader to unit test DAL

I have a method at my data access level that does the mapping. The method accepts a DataReader and compares the data with the corresponding properties of the domain object. Is there a good way to somehow make fun of the DataReader so that I can run unit tests of matching methods without getting into a physical database?

+6
c #
source share
3 answers

Fortunately for you, DataReader implements the IDataReader interface.

Instead of relying on DataReader in your code, use IDataReader . Then, in your tests, you can replace your own implementation that returns dummy data, or use a fake framework like Rhino.Mocks or similar to create stubs and assign return values.

Depending on how you “get” the DataReader in your code, you may need a little refactoring. You want external dependencies, such as "injected" in the constructor (preferable) or through a property, so that class consumers can replace any implementation of IDataReader . (This replacement is also the reason that you declare your parameters / properties as abstractions, not specific types.) This is called Dependency Injection , a form of Inversion of Control .

+11
source share

Yes, do not use DataReader , but IDataReader or IDataRecord , then scoff at what you want.

+3
source share

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 :)

+3
source share

All Articles