<p>As a novice in practicing test-driven development, I often end up in a quandary as to how to unit test persistence to a database.</p>

<p>I know that technically this would be an integration test (not a unit test), but I want to find out the best strategies for the following:</p>

<ol>
<li>Testing queries. </li>
<li>Testing inserts. How do I know that the insert that has gone wrong if it fails? I can test it by inserting and then querying, but how can I know that the query wasn't wrong? </li>
<li>Testing updates and deletes -- same as testing inserts</li>
</ol>

<p>What are the best practices for doing these?</p>

<hr>

<p>Regarding testing SQL: I am aware that this could be done, but if I use an O/R Mapper like NHibernate, it attaches some naming warts in the aliases used for the output queries, and as that is somewhat unpredictable I'm not sure I could test for that.</p>

<p>Should I just, abandon everything and simply trust NHibernate? I'm not sure that's prudent.</p>

[{'answer_id': 2049, 'author': 'Rytmis', 'author_id': 266, 'author_profile': 'https://Stackoverflow.com/users/266', 'pm_score': 2, 'selected': False, 'text': "<p>You do the unit testing by mocking out the database connection. This way, you can build scenarios where specific queries in the flow of a method call succeed or fail. I usually build my mock expectations so that the actual query text is ignored, because I really want to test the fault tolerance of the method and how it handles itself -- the specifics of the SQL are irrelevant to that end. </p>\n\n<p>Obviously this means your test won't actually verify that the method <em>works</em>, because the SQL may be wrong. This is where integration tests kick in. For that, I expect someone else will have a more thorough answer, as I'm just beginning to get to grips with those myself.</p>\n"}, {'answer_id': 2066, 'author': 'David Sykes', 'author_id': 259, 'author_profile': 'https://Stackoverflow.com/users/259', 'pm_score': 1, 'selected': False, 'text': '<p>I would also mock the database, and check that the queries are what you expected. There is the risk that the test checks the wrong sql, but this would be detected in the integration tests</p>\n'}, {'answer_id': 2073, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 2, 'selected': False, 'text': "<p>The problem I experienced when unit testing persistence, especially without an ORM and thus mocking your database (connection), is that you don't really know if your queries succeed. It could be that you your queries are specifically designed for a particular database version and only succeed with that version. You'll never find that out if you mock your database. So in my opinion, unit testing persistence is only of limited use. You should always add tests running against the targeted database.</p>\n"}, {'answer_id': 2096, 'author': 'Rytmis', 'author_id': 266, 'author_profile': 'https://Stackoverflow.com/users/266', 'pm_score': 2, 'selected': False, 'text': "<p>For <code>NHibernate</code>, I'd definitely advocate just mocking out the <code>NHibernate API</code> for unit tests -- trust the library to do the right thing. If you want to ensure that the data actually goes to the DB, do an integration test.</p>\n"}, {'answer_id': 2099, 'author': 'Dan', 'author_id': 230, 'author_profile': 'https://Stackoverflow.com/users/230', 'pm_score': 1, 'selected': False, 'text': '<p>Technically unit tests of persistance are not unit tests. They are integration tests.</p>\n<p>With C# using mbUnit, you simply use the <code>SqlRestoreInfo</code> and <code>RollBack</code> attributes:</p>\n<pre><code>    [TestFixture]\n    [SqlRestoreInfo(&lt;connectionsting&gt;, &lt;name&gt;,&lt;backupLocation&gt;]\n    public class Tests\n    {\n      \n        [SetUp]\n        public void Setup()\n        {\n        \n        }\n\n        [Test]\n        [RollBack]\n        public void TEST()\n        {\n           //test insert. \n        }\n    }\n</code></pre>\n<p>The same can be done in NUnit, except the attribute names differ slightly.</p>\n<p>As for checking, if your query iss successful, you normally need to follow it with a second query to see if the database has been changed as you expected.</p>\n'}, {'answer_id': 7641, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 5, 'selected': True, 'text': '<p>Look into DB Unit.  It is a Java library, but there must be a C# equivalent.  It lets you prepare the database with a set of data so that you know what is in the database, then you can interface with DB Unit to see what is in the database.  It can run against many database systems, so you can use your actual database setup, or use something else, like HSQL in Java (a Java database implementation with an in memory option).</p>\n\n<p>If you want to test that your code is using the database properly (which you most likely should be doing), then this is the way to go to isolate each test and ensure the database has expected data prepared.</p>\n'}, {'answer_id': 9316, 'author': 'RobertTheGrey', 'author_id': 1107, 'author_profile': 'https://Stackoverflow.com/users/1107', 'pm_score': 2, 'selected': False, 'text': '<p>I have written a post here concerning <a href="http://buffered.io/posts/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/" rel="nofollow noreferrer">unit testing the data layer</a> which covers this exact problem. Apologies for the (shameful) plug, but the article is too long to post here.</p>\n\n<p>I hope that helps you - it has worked very well for me over the last 6 months on 3 active projects.</p>\n\n<p>Regards,</p>\n\n<p>Rob G</p>\n'}, {'answer_id': 26017, 'author': 'Josh Brown', 'author_id': 2030, 'author_profile': 'https://Stackoverflow.com/users/2030', 'pm_score': 4, 'selected': False, 'text': '<p>As <a href="https://stackoverflow.com/questions/2046/how-do-i-unit-test-persistence#7641">Mike Stone said</a>, DbUnit is great for getting the database into a known state before running your tests.  When your tests are finished, DbUnit can put the database back into the state it was in before you ran the tests.</p>\n\n<p><a href="http://www.dbunit.org/" rel="nofollow noreferrer">DbUnit (Java)</a></p>\n\n<p><a href="http://dbunit-net.sourceforge.net/" rel="nofollow noreferrer">DbUnit.NET</a></p>\n'}, {'answer_id': 31313, 'author': 'Thomas Eyde', 'author_id': 3282, 'author_profile': 'https://Stackoverflow.com/users/3282', 'pm_score': 1, 'selected': False, 'text': '<p>I usually create a repository, use that to save my entity and retrieve a fresh one. Then I assert that the retrieved is equal to the saved.</p>\n'}, {'answer_id': 24649805, 'author': 'cchantep', 'author_id': 3347384, 'author_profile': 'https://Stackoverflow.com/users/3347384', 'pm_score': 2, 'selected': False, 'text': '<p>For JDBC based projects, my Acolyte framework can be used: <a href="http://acolyte.eu.org" rel="nofollow noreferrer">http://acolyte.eu.org</a> . It allows to mockup data access you want to tests, benefiting from JDBC abstraction, without having to manage a specific test DB.</p>\n'}]
['https://Stackoverflow.com/questions/2046', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/372/']