Rails 将测试期间通过保存 activerecord 对象创建的数据存储在哪里? [英] Where does Rails store data created by saving activerecord objects during tests?

查看:16
本文介绍了Rails 将测试期间通过保存 activerecord 对象创建的数据存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

$ rails -d mysql 测试

$ cd 测试

$ nano config/database.yml ...

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

$ 脚本/生成 rspec

$ 脚本/生成 rspec_model foo

编辑 Foo 迁移:

<前>类 CreateFoos

$ rake db:migrate

编辑规范/模型/foo_spec.rb:

<前>需要 File.expand_path(File.dirname(__FILE__) + '/../spec_helper')描述 Foo之前(:每个)做@valid_attributes = {:bar => 12345}结尾它应该创建一个给定有效属性的新实例"做foo = Foo.new(@valid_attributes)保存把睡觉……"睡觉(20)结尾结尾

$ 佣金规范

当你看到sleeping..."时,切换到另一个打开的终端,将 mysql 会话连接到 test_test 数据库,然后执行:

mysql> select * from foos;空集(0.00 秒)

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

解决方案

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

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

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

self.use_transactional_fixtures = false

代替

self.use_transactional_fixtures = true

它会导致您的测试数据库中的数据持久化.

另外:我的建议是专门为使用 Test::Unit 而设计的,而不是 RSpec.但是,我想您应该寻找 spec_helper.rb 中的类似设置.

Where does Rails store data created by saving activerecord objects during tests?

I thought I knew the answer to that question: obviously in the _test database. But it looks like this is not true!

I used this system to test what's happening to saved ActiveRecord data during rspec tests:

$ rails -d mysql test

$ cd test

$ nano config/database.yml ...

... create mysql databases test_test, test_development, test_production

$ script/generate rspec

$ script/generate rspec_model foo

edit Foo migration:

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

$ rake spec

When you see "sleeping...", change to another open terminal with a mysql session conneted to the test_test database and do:

mysql> select * from foos; Empty set (0.00 sec)

Why doesn't the mysql session show any records in the test_test database while the test is running?

解决方案

Items in the test database are erased by default after each test is run, by design. This is done to make sure that each of your tests has its own sandbox to play in that doesn't cause any interactions with the tests before it.

Again, this is by design. You don't want tests that are manipulating the same set of data (or rely on synchronous execution), because there is no guarantee of execution order.

However, I believe if you modify you test/test_helper.rb file to say this:

self.use_transactional_fixtures = false

instead of

self.use_transactional_fixtures = true

It will cause the data in your test database to persist.

ALSO: My advice is specifically designed to work with Test::Unit, not RSpec. However, I imagine that there is a similar setting your spec_helper.rb you should be looking for.

这篇关于Rails 将测试期间通过保存 activerecord 对象创建的数据存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆