ruby-on-rails - Rails 在哪里存储通过在测试期间保存 activerecord 对象创建的数据?

标签 ruby-on-rails ruby database rspec testing

Rails 在哪里存储测试期间通过保存 activerecord 对象创建的数据?

我以为我知道这个问题的答案:显然在_test 数据库 中。但看起来这不是真的!

我使用这个系统来测试在 rspec 测试期间保存的 ActiveRecord 数据发生了什么:

$ rails -d mysql 测试

$ 光盘测试

$ nano config/database.yml ...

... 创建 mysql 数据库 test_test、test_development、test_production

$脚本/生成rspec

$ 脚本/生成 rspec_model foo

编辑 Foo 迁移:

class CreateFoos 

$ rake db:migrate

edit spec/models/foo_spec.rb:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Foo do
  before(:each) do
    @valid_attributes = {
      :bar => 12345
    }
  end

  it "should create a new instance given valid attributes" do
    foo = Foo.new(@valid_attributes)
    foo.save
    puts "sleeping..."
    sleep(20)
  end
end

$ 佣金规范

当你看到“sleeping...”时,切换到另一个带有连接到 test_test 数据库的 mysql session 的打开终端,然后执行:

mysql> 从 foos 中选择 *; 空集(0.00 秒)

为什么 mysql session 在测试运行时不显示 test_test 数据库中的任何记录?

最佳答案

测试数据库中的项目在每次测试运行后默认被删除,这是设计的。这样做是为了确保您的每个测试都有自己的沙盒可以在其中运行,不会与之前的测试发生任何交互。

同样,这是设计使然。您不希望操作同一组数据(或依赖同步执行)的测试,因为无法保证执行顺序。

但是,我相信如果你修改你的 test/test_helper.rb 文件这样说:

self.use_transactional_fixtures = false

代替

self.use_transactional_fixtures = true

它会导致你的测试数据库中的数据持久化。

此外:我的建议专门针对 Test::Unit 而不是 RSpec。但是,我想您应该寻找一个类似的设置您的 spec_helper.rb。

关于ruby-on-rails - Rails 在哪里存储通过在测试期间保存 activerecord 对象创建的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/437760/

相关文章:

jquery - Bootstrap 下拉按钮菜单消失

ruby-on-rails - 选择在 Rails 的列中具有最高值的记录

ruby-on-rails - 在本地主机上设置 Rails 项目,postgresql 问题

php - 无法仅在 cPanel 上连接到数据库

ruby-on-rails - 删除 active_admin_editor gem 上的按钮

jquery - Rails 4 应用程序中的动态可折叠列表

ruby-on-rails - RoR : has_one "or the other"?(或者,没有继承的多态性。)

ruby - Ruby TCPSocket 超时是如何定义的?

database - 打开 Mac 版 DBeaver 时出错 : You can’t open the application “DBeaver” because it is not supported on this type of Mac

mysql - 我们可以在数据库中存储多个数据条目,只对应一个主键/序列 ID 吗?