I am taking my first steps with MsTest and Moq and would like to unit test a Linq2SQL repository class. The problem is that I do not want the unit tests to permantly modify my development database.
Which would be the best approach for this scenario?
- Let each test operate on my real development database, but make sure each test cleans up after itself
- Create a duplicate of my development database and dbml for the unit test and use that context instead so I can clear the entire database before each test run
- Find some elaborate way of mocking the Datacontext (please bear in mind that I am a total Moq noob).
- Something completely different? Perhaps something that would automate setting up the database for me before each test run?
Edit: I just learned that MBUnit has a rollback attribute that reverses any database operations run by a test case. I am not particularly attached to MSTest, so could this be an easy answer to my problem?
I played a bit with MBUnit and learned that, for most test cases, you can get away without mocking the datacontext by using MBUnit’s [ROLLBACK] attribute.
Unfortunately there are also cases when the attribute produces strange side effects, such as loading a linq entity from the database, changing one property (without submitchanges), then loading the same entity again. Usually this results in no update query on the database, but from within the Test Method it appears as if the update is immediately executed as soon as I change the linq entity property.
Not a perfect solution, but I think I’ll go with the [ROLLBACK] attribute since it’s less effort and works well enough for me.