I'm planning on writing unit tests for a REST-ful API and I'm wondering about the approach I should take.
The aspect that concerns me the most is related to the database state. My understanding is that the environment, or the initial state, of a test target should be the same for each test, which means that the database should also be the same for each test. How do I achieve that when I have a heavy database? Also, how do I handle eventual changes in the database schema?
Usually, in unit tests, you try to remove all dependencies beyond the unit (usually a function/class/object) that you are testing. The classic way to accomplish this in your case (for databases) is through the use of mocks. http://en.wikipedia.org/wiki/Mock_object
Basically, you implement a "FakeDatabase" that shares the API of the actual database that returns known values.
Question is what do you want to test. What do you think will break at the topmost api-layer (i.e. layer which is receiving HTTP request)?
Generally writing a unit-test restful-api sounds a bit like an oxymoron ;) By definition unit-tests are much smaller as using entry-point HTTP to database. It sounds more that your question is based on how to write large-tests (or acceptance, end-to-end test).
Beware that implementing such large-tests (end-to-end test) involves high effort:
In my experience diversity of test-granularity is king, therefore I mix/combine approaches:
Maybe you can give more input on a specific use-case you want to test, so can give more help?