Unit testing mappings using PersistenceSpecification and more "classic" tests
Hi,
I'm having a little question concerning Unit Testing the database access. Basically we have tried creating unit tests using PersistenceSpecification to test the mapping but we would like to add some other tests, like testing if the methode GetAllTags() does actually work and returns the expected results.
What we are using for those unit testing is an in-memory database using SQLite, we are using the repository pattern, so in this case we are testing the repository. We have a class that generates the in-memory database, creates the structure and sends back the ISession that is used in the repository.
The trouble is that we want to add some data in the in-memory database to ease the unit tests, so that we can test that GetTagsForUser(string user) actually returns only the tags for the specified user and returns all of them, for example. This seems to create some troubles to the PersistenceSpecification testing, making it fail.
If we try our unit tests wihtout the one using PersistenceSpecification, they all pass correctly, if we try the PersistenceSpecification unit tests without the others, they pass as well. It's only if we try to run both that we have trouble, some tests not passing, and the explanation doesn't help (it states something that the database shouldn't be flushed so that we see the trouble).
What I would like to know is how and where we should be generating the data for the unit tests, and if there is a better way than doing it after the creating of the in-memory database.
Thanks in advance for your returns.
Support Staff 2 Posted by James Gregory on 23 Mar, 2010 08:46 PM
I've not heard of anyone having any problems running tests with the PersistenceSpecification alongside regular integration tests; however, that could just be because nobody is doing it!
My first suggestion would be to create separate databases for your different tests, or at least recreate it; in-memory databases are pretty cheap, so this shouldn't really slow down your tests. The tests that use the PersistenceSpecification also don't require any data, so you could skip that for them.
As for generating the data, there's no real recommended way to do it. Bare in mind that the in-memory databases are destroyed when the session is closed; but apart from that, just do whatever works.