changing the database before every test
I am writing some profiling tests for certain methods like adding customers (different batch sizes 100, 1000, 10000) to customers table in the database. I am using S开发者_JAVA技巧qlCE database because of its support from LINQ.
Now to reach the ideal case, i am changing the database before every test so that every test will have same database. for that i am using File.Copy() but while using db.customers.FirstOrDefault() in the second test, i am getting the error "The database file may be corrupted. Run the repair utility to check the database file."
I am clearing the data context after the test. The problem also may be with linq. I am not sure.Any idea of solving this. or any alternative to this approach. Any suggestions are required.
You should not do unit testing like that, it is error-prone and slow.
Instead, use database transactions, and rollback the data at the end of the tests, or when required (say per test).
An alternative might be to have a function that clears the data in the db (or in specific tables) and populates it to your desired start-state. This would then be run before you begin your tests (or before individual tests, depending on the requirements). Using this method would make File.Copy()
unnecessary.
You can use SQLite with a in-memory database.
SQLiteConnection conn = new SQLiteConnection("Data Source = :memory:");
ps: keep leppie answer in mind
精彩评论