简体   繁体   English

使用现有数据库中的数据在ASP.NET MVC中创建数据库模拟

[英]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.. 我有一个现有的ASP.NET MVC应用程序,在SQL Server数据库中有一些示例数据,效果很好。

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? 假设我已经准备好所有必需的存储库和IOC,是否有一个工具可以从一组表中提取数据,并将其“冻结”到一个模拟对象中(也许使用XML文件来存储数据) ,以便我可以分离数据库并将模拟数据用于单元测试?

Depending on what exactly you are trying to test there might be different approaches. 根据您要测试的内容,可能会有不同的方法。

If you want to test the data access logic then this is no longer unit test but integration test. 如果要测试数据访问逻辑,则这不再是单元测试,而是集成测试。 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. 对于这类测试,最好是用一些更轻巧的内存数据库(例如SQLite)轻松替换实际数据库,该数据库将在运行每个测试之前重新构建。 If you are using an ORM this task is easy. 如果您使用的是ORM,则此任务很容易。 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. 您需要做的就是从现有数据库中生成SQL脚本(INSERT INTO ...),修改方言并使之适应SQLite(如有必要),读取并注入到SQLite文件中,最后剩下的就是指示您数据访问层,以使用SQLite方言和连接字符串进行单元测试。

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. 现在,如果您不使用ORM,并且您的数据访问逻辑与MSSQL绑定在一起,那么事情变得更糟了,您将需要一个实时数据库来执行那些集成测试。 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. 再次,您将需要正确地setupteardown (最好在事务中)此测试数据库,以使其处于测试的已知状态。

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. 我不知道您要做什么的直接方法,但是MSSQL支持导出到CSV,Access和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? 仅在与新数据库相同的SQL Server上或在开发SQL服务器上还原数据库的备份是否可行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ASP.net MVC从数据库中获取数据? - How to fetch data from database using ASP.net MVC? 将现有的SqlMembershipProvider数据库与ASP.NET MVC应用程序一起使用 - Using an existing SqlMembershipProvider database with an ASP.NET MVC app ASP.NET MVC 4 从现有数据库中搜索 - ASP.NET MVC 4 searching from existing database 使用asp.net mvc 5为数据库创建Web界面 - Creating a web interface for a database using asp.net mvc 5 使用已经准备好的迁移ASP.NET MVC创建数据库 - Creating database using already prepared migrations ASP.NET MVC 如何使用LINQ查询数据库以基于ASP.Net MVC 5中的月份数组从数据库中获取数据? - How to query database using LINQ to bring data from database based on array of months in ASP.Net MVC 5? 如何使用ASP.NET MVC将数据从一个数据库表移动到同一数据库的另一表? - How to move data from one database table to another table of same database using ASP.NET MVC? 数据库(ASP.NET MVC)中缺少来自集合的数据 - Data from Collection is missing from database (ASP.NET MVC) 使用 AJAX (ASP.NET MVC) 将数据插入数据库 - Insert data to database using AJAX (ASP.NET MVC ) 如何在 ASP.NET MVC 中使用 HttpGet 将数据保存到数据库中? - How to save data into database using HttpGet in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM