I have a ASP.NET MVC3 application, where i need to test a database wrapper class. This is up and running. But i need to recreate the database, when the test starts. I need to wipe all data in database. The database is a test double.
I have the SQL script creating the database. For now im running this manualy in “SQL Server Manager” and it works. But I would like to have the script run before a new test run.
Anyone know how to do this?
EDIT – Replacing with a decent answer.
Create a C# class that looks like this –
That should run setup once only before any tests, and then teardown once only after all tests have completed.
I realize I’m raising this one from the dead but I felt it was time it got a decent answer.
In any case, there is a much better way. Mock your datalayer and test that. Then make certain everything done with SQL Server is in the form of Stored Procedures and Functions. Then use Visual Studio to create unit tests for those sprocs and functions.
If you have logic in your repositories, I recommend grabbing Insight.Database and making use of its AutoInterface feature. That way, your repository layer is merely an interface, which you can easily mock and move on.