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

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

问题描述

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

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

我认为我知道这个问题的答案:显然在 _test数据库

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

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

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

$ rails -d mysql test

$ rails -d mysql test

$ cd test

$ cd test

$ nano config / database.yml ...

$ nano config/database.yml ...

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

... create mysql databases test_test, test_development, test_production

$ script / generate rspec

$ script/generate rspec

$ script / generate rspec_model foo

$ script/generate rspec_model foo

edit Foo migration:

edit Foo migration:


class CreateFoos 

$ rake db:migrate

edit spec / models / foo_spec.rb:

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

$ rake spec

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

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

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

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.

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

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

self.use_transactional_fixtures = false

而不是

self.use_transactional_fixtures = true

这将导致测试数据库中的数据持久化。

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

另外:我的建议是专门设计用于与Test :: Unit,而不是RSpec。但是,我想你有一个类似的设置你的spec_helper.rb你应该寻找。

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天全站免登陆