简体   繁体   English

单元测试MVC数据库

[英]Unit Testing MVC Database

I have an MVC Application along with a unit-testing project with a reference to my main project. 我有一个MVC应用程序以及一个单元测试项目,并引用了我的主项目。 Presently all my unit tests that involve database work are performed on the main application database. 目前,我所有涉及数据库工作的单元测试都在主应用程序数据库上执行。

Would it be best practice to create a replica db instead and store it in the testing project? 最好的做法是创建一个副本数据库并将其存储在测试项目中吗?

When i test creating new users i would not like them to be a part of the main database. 当我测试创建新用户时,我不希望他们成为主数据库的一部分。 Is a replica database a good solution or would it be sufficient to delete anything I create in my teardown methods? 副本数据库是一个好的解决方案,还是删除我在拆解方法中创建的任何内容就足够了?

Any help would be great! 任何帮助将是巨大的!

I think running your unit tests on the main application database is not a good idea. 我认为在主应用程序数据库上运行单元测试不是一个好主意。 Your unit tests will be more complex and you can get interdependence issues where forgetting to delete data from one test breaks another further down the track. 您的单元测试将更加复杂,并且您会遇到相互依赖的问题,其中忘记从一项测试中删除数据会破坏另一项。 These can be time consuming to track down and fix. 这些可能非常耗时,无法进行跟踪和修复。

You can also end up with old test data polluting the database and increasing its size and requiring periodic cleanup. 您还可能最终得到旧的测试数据,这会污染数据库并增加其大小,并需要定期进行清理。 I have used both methods and find that creating a new database for every test or suite of tests is far more workable and easier to maintain. 我已经使用了这两种方法,并且发现为每个测试或测试套件创建一个新的数据库更加可行并且更易于维护。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM