Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When a test executes against a database through another piece of code it ceases to be an unit test. It is called an "<a href="http://en.wikipedia.org/wiki/Integration_testing" rel="nofollow noreferrer">integration test</a>" because you are testing the interactions of different pieces of code and how they "integrate" together. Not that it really matters, but its fun to know.</p> <p>When you perform a test, the following things should occur:</p> <ol> <li>Begin a db transaction</li> <li>Insert known (possibly bogus) test items/entities</li> <li>Call the (one and only one) function to be tested</li> <li>Test the results</li> <li>Rollback the transaction</li> </ol> <p>These things should happen for each and every test. With NUnit, you can get away with writing step 1 and 5 just once in a base class and then inheriting from that in each test class. NUnit will execute Setup and Teardown decorated methods in a base class.</p> <p>In step 2, if you're using SQL, you'll have to write your queries such that they return the PK numbers back to your test code.</p> <pre><code>INSERT INTO Person(FirstName, LastName) VALUES ('Fred', 'Flintstone'); SELECT SCOPE_IDENTITY(); --SQL Server example, other db vendors vary on this. </code></pre> <p>Then you can do this</p> <pre><code>INSERT INTO Person(FirstName, LastName, SpouseId) VALUES('Wilma', 'Flintstone', @husbandId); SET @wifeId = SCOPE_IDENTITY(); UPDATE Person SET SpouseId = @wifeId WHERE Person.Id = @husbandId; SELECT @wifeId; </code></pre> <p>or whatever else you need.</p> <p>In step 4, if you use SQL, you have to re-SELECT your data and test the values returned.</p> <p>Steps 2 and 4 are less complicated if you are lucky enough to be able to use a decent ORM like (N)Hibernate (or whatever).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload