<p>So I'm working on some legacy code that's heavy on the manual database operations. I'm trying to maintain some semblance of quality here, so I'm going TDD as much as possible.</p>

<p>The code I'm working on needs to populate, let's say a <code>List&lt;Foo&gt;</code> from a DataReader that returns all the fields required for a functioning Foo. However, if I want to verify that the code in fact returns one list item per one database row, I'm writing test code that looks something like this:</p>

<pre><code>Expect.Call(reader.Read()).Return(true);
Expect.Call(reader["foo_id"]).Return((long) 1);
// ....
Expect.Call(reader.Read()).Return(true);
Expect.Call(reader["foo_id"]).Return((long) 2);
// ....
Expect.Call(reader.Read()).Return(false);
</code></pre>

<p>Which is rather tedious and rather easily broken, too. </p>

<p>How should I be approaching this issue so that the result won't be a huge mess of brittle tests?</p>

<p>Btw I'm currently using Rhino.Mocks for this, but I can change it if the result is convincing enough. Just as long as the alternative isn't TypeMock, because their EULA was a bit too scary for my tastes last I checked.</p>

<p>Edit: I'm also currently limited to C# 2.</p>

[{'answer_id': 30055, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 0, 'selected': False, 'text': '<p>You can put the Foo instances in a list and compare the objects with what you read:  </p>\n\n<pre><code>var arrFoos = new Foos[]{...}; // what you expect\nvar expectedFoos = new List&lt;Foo&gt;(arrFoos); // make a list from the hardcoded array of expected Foos\nvar readerResult = ReadEntireList(reader); // read everything from reader and put in List&lt;Foo&gt;\nExpect.ContainSameFoos(expectedFoos, readerResult); // compare the two lists\n</code></pre>\n'}, {'answer_id': 30635, 'author': 'Rytmis', 'author_id': 266, 'author_profile': 'https://Stackoverflow.com/users/266', 'pm_score': 0, 'selected': False, 'text': "<p>Kokos,</p>\n\n<p>Couple of things wrong there. First, doing it that way means I have to construct the Foos first, then feed their values to the mock reader which does nothing to <em>reduce</em> the amount of code I'm writing. Second, if the values pass through the reader, the Foos won't be the <em>same</em> Foos (reference equality). They might be <em>equal</em>, but even that's assuming too much of the Foo class that I don't dare touch at this point.</p>\n"}, {'answer_id': 30678, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 2, 'selected': True, 'text': "<p>To make this less tedious, you will need to encapsulate/refactor the mapping between the DataReader and the Object you hold in the list.  There is quite of few steps to encapsulate that logic out.  If that is the road you want to take, I can post code for you.  I am just not sure how practical it would be to post the code here on StackOverflow, but I can give it a shot to keep it concise and to the point.  Otherwise, you are stuck with the tedious task of repeating each expectation on the index accessor for the reader.  The encapsulation process will also get rid of the strings and make those strings more reusable through your tests.</p>\n\n<p>Also, I am not sure at this point how much you want to make the existing code more testable.  Since this is legacy code that wasn't built with testing in mind.</p>\n"}, {'answer_id': 30703, 'author': 'Toran Billups', 'author_id': 2701, 'author_profile': 'https://Stackoverflow.com/users/2701', 'pm_score': 0, 'selected': False, 'text': '<p>Just to clarify, you want to be able to test your call into SQL Server returned some data, or that if you had some data you could map it back into the model?</p>\n\n<p>If you want to test your call into SQL returned some data checkout my answer found <a href="https://stackoverflow.com/questions/12374/has-anyone-had-any-success-in-unit-testing-sql-stored-procedures#25204">here</a></p>\n'}, {'answer_id': 30741, 'author': 'Rytmis', 'author_id': 266, 'author_profile': 'https://Stackoverflow.com/users/266', 'pm_score': 0, 'selected': False, 'text': "<p>@Toran: What I'm testing is the programmatic mapping from data returned from the database to quote-unquote domain model. Hence I want to mock out the database connection. For the other kind of test, I'd go for all-out integration testing.</p>\n\n<p>@Dale: I guess you nailed it pretty well there, and I was afraid that might be the case. If you've got pointers to any articles or suchlike where someone has done the dirty job and decomposed it into more easily digestible steps, I'd appreciate it. Code samples wouldn't hurt either. I do have a clue on how to approach that problem, but before I actually dare do that, I'm going to need to get other things done, and if testing that will require tedious mocking, then that's what I'll do.</p>\n"}, {'answer_id': 30844, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 1, 'selected': False, 'text': '<p>I thought about posting some code and then I remembered about JP Boodhoo\'s Nothin But .NET course.  He has a <a href="http://code.google.com/p/jpboodhoo/" rel="nofollow noreferrer">sample project</a> that he is sharing that was created during one of his classes.  The project is hosted on <a href="http://code.google.com/" rel="nofollow noreferrer">Google Code</a> and it is a nice resource.  I am sure it has some nice tips for you to use and give you ideas on how to refactor the mapping.  The whole project was built with TDD.</p>\n'}]
['https://Stackoverflow.com/questions/29980', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/266/']