简体   繁体   English

没有数据库设置或拆卸的Rails单元测试?

[英]Rails unit tests without db setup or teardown?

I want to write a few unit tests that do not make any changes to a database. 我想编写一些不对数据库进行任何更改的单元测试。

I have a Rails 2.3.11 application. 我有一个Rails 2.3.11应用程序。 This app has a SQLite database as its primary database. 此应用程序具有一个SQLite数据库作为其主数据库。 In many ways, this is a run-of-the-mill Rails app. 从许多方面来说,这是一个常规的Rails应用程序。

What makes this app unique is that it also establishes a connection to a SQL Server database. 该应用程序与众不同的原因在于,它还建立了与SQL Server数据库的连接。 I have some models which are abstract classes and they use the SQL Server database. 我有一些模型是抽象类,它们使用SQL Server数据库。 I have before_save and before_destroy callbacks to prevent any changes being made to the SQL Server database. 我具有before_savebefore_destroy回调,以防止对SQL Server数据库进行任何更改。 Also, the user credentials to connect to SQL Server are supposed to be read-only. 同样,连接到SQL Server的用户凭据应该是只读的。

I would like to write unit tests that make assertions on the data that is already present in the SQL Server database. 我想编写对SQL Server数据库中已经存在的数据进行断言的单元测试。 But I don't want to setup or teardown the SQL Server database. 但是我不想设置或拆除SQL Server数据库。

I am afraid to just see what happens. 恐怕只能看看会发生什么。 I would like to have a setting in the unit test that will prevent Rails from trying to setup or teardown the SQL Server database. 我想在单元测试中有一个设置,以防止Rails尝试设置或拆除SQL Server数据库。 Is this possible? 这可能吗? How do I do it? 我该怎么做?

Thank you! 谢谢!

The setup/tear down only affects the application database (SQLite, sounds like), not additional, external database connections. 设置/删除仅影响应用程序数据库(SQLite,听起来像),而不影响其他外部数据库连接。

Also, you should keep your test environment completely separate from your production environment. 另外,您应该使测试环境与生产环境完全分开。 So, if you're using a test SQLServer DB as well (and you should be, with test data in it - not the production one) then you should be fine even if the worst happens. 因此,如果您还使用测试SQLServer DB(并且应该在其中包含测试数据,而不是生产数据),那么即使发生最坏的情况,也应该没问题。

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

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