Created
September 13, 2012 12:05
-
-
Save serbrech/3713904 to your computer and use it in GitHub Desktop.
DataReaderTestHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class DataReaderTestHelper | |
| { | |
| private readonly string _tableName; | |
| public abstract IEnumerable<string> GetColumns(); | |
| protected DataReaderTestHelper(string tableName = "fakeTable") | |
| { | |
| _tableName = tableName; | |
| } | |
| public DataTable GetDataTable(object values) | |
| { | |
| var table = new DataTable(); | |
| table.TableName = _tableName; | |
| //Add a column for each defined column | |
| foreach (var column in GetColumns()) | |
| { | |
| table.Columns.Add(new DataColumn(column)); | |
| } | |
| var row = GetRow(table, values); | |
| table.Rows.Add(row); | |
| return table; | |
| } | |
| public IDataReader GetDataReader(object values) | |
| { | |
| return new DataTableReader(GetDataTable(values)); | |
| } | |
| public DataRow GetRow(DataTable table, object values) | |
| { | |
| var row = table.NewRow(); | |
| var type = values.GetType(); | |
| foreach (var prop in type.GetProperties()) | |
| { | |
| row[prop.Name] = prop.GetValue(values, null); | |
| } | |
| return row; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class PersonDataReaderTestHelper : DataReaderTestHelper | |
| { | |
| public override IEnumerable<string> GetColumns() | |
| { | |
| return new[] | |
| { | |
| "FirstName", | |
| "LastName" | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void Should_map_person_properties() | |
| { | |
| var personRow = new | |
| { | |
| FirstName = "firstname", | |
| LastName = "lastname" | |
| }; | |
| var reader = new PersonDataReaderTestHelper() | |
| .GetDataReader(personRow); | |
| var person = new PersonMapper().Map(personRow); | |
| person.FirstName.Should().Be(personRow.FirstName); | |
| person.LastName.Should().Be(personRow.LastName); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment