kala-tamin Creating Database Mocks in ASP.NET MVC using Data from Existing Database

Creating Database Mocks in ASP.NET MVC using Data from Existing Database

I have an existing ASP.NET MVC application with some sample data in the SQL Server database, which is working fine..

Assuming I have all of the necessary repositories and IOC in place, is there a tool that will extract the data from a group of tables, and "freeze-dry" it into a mock object (perhaps using an XML file to store the data), so that I can detach the database and use the mock data for my unit tests?

How do I specify error member key in ASP.NET MVC CustomValidation?


MVC HandleError attribute not working on Controller
Depending on what exactly you are trying to test there might be different approaches.

What is the elegant solution for unrelated views in MVC web frameworks?
Setting width and height of modal window from JavaScript/JQuery to ASP.NET MVC inline code
If you want to test the data access logic then this is no longer unit test but integration test.

How can I implement a label file for regions that integrates well with MVC application structure?
For this kind of tests it is a good idea to be able to easily replace the real database with some lighter maybe even in-memory database like SQLite which will be reconstructed before running each test.

How asp.net MVC's cache and Firefox's cache work?
If you are using an ORM this task is easy.

Is the MVC Controller Layer the ideal place for error handling?
All you need to do is to generate SQL scripts (INSERT INTO...) from your existing database, modify and adapt the dialect to SQLite (if necessary), read and inject into a SQLite file and finally all that's left is to instruct your data access layer to use SQLite dialect and connection string for the unit test..
asp.net mvc: read route param inside controller
Now if you are not using an ORM and your data access logic is tied to MSSQL things get uglier you will need a live database in order to perform those integration tests.

In this case I would suggest you duplicate your real database which you would use for the tests by modifying only the connection string.

Once again you will need to properly setup and teardown (preferably in a transaction) this test database in order to put it into a known state for the tests.. If you want to test code that depends on those repositories (such as your controllers for example) you don't need to even bother about mocking the data as your controllers depend on abstract repositories and not the real implementations (aren't they), so you could easily mock the methods of the repository in order to test the logic in the controllers..



I don't know of a direct way to do what you're asking for, but MSSQL supports export to CSV, Access, and Excel.

Although, this require you to change the Data Access Layer in your in your mid-tier, and furthermore, I don't feel this answers your question:.
"freeze-dry" it into a mock object.
This I'm not sure of.

Is it feasible to just restore a backup of the database either on the same SQL server as a new database, or possibly on a dev SQL server?.

89 out of 100 based on 79 user ratings 1229 reviews