Rails 单元测试无需数据库设置或拆卸?
我想编写一些不对数据库进行任何更改的单元测试。
我有一个 Rails 2.3.11 应用程序。该应用程序有一个 SQLite 数据库作为其主数据库。从很多方面来说,这是一个普通的 Rails 应用程序。
这个应用程序的独特之处在于它还建立了与 SQL Server 数据库的连接。我有一些模型是抽象类,它们使用 SQL Server 数据库。我有 before_save
和 before_destroy
回调来防止对 SQL Server 数据库进行任何更改。此外,连接到 SQL Server 的用户凭据应该是只读的。
我想编写单元测试来对 SQL Server 数据库中已存在的数据进行断言。但我不想设置或拆除 SQL Server 数据库。
我害怕只是看看会发生什么。我希望在单元测试中进行一个设置,以防止 Rails 尝试设置或拆除 SQL Server 数据库。这可能吗?我该怎么做?
谢谢你!
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. This app has a SQLite database as its primary database. In many ways, this is a run-of-the-mill Rails app.
What makes this app unique is that it also establishes a connection to a SQL Server database. I have some models which are abstract classes and they use the SQL Server database. I have before_save
and before_destroy
callbacks to prevent any changes being made to the SQL Server database. Also, the user credentials to connect to SQL Server are supposed to be read-only.
I would like to write unit tests that make assertions on the data that is already present in the SQL Server database. But I don't want to setup or teardown the SQL Server database.
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. Is this possible? How do I do it?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
安装/拆卸仅影响应用程序数据库(听起来像 SQLite),而不影响其他外部数据库连接。
此外,您应该将测试环境与生产环境完全分开。因此,如果您也使用测试 SQLServer DB(您应该使用测试数据,而不是生产数据),那么即使最坏的情况发生,您也应该没问题。
The setup/tear down only affects the application database (SQLite, sounds like), not additional, external database connections.
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.