RSpec是否会在每次阻止后销毁FactoryGirl创建的对象?

时间:2015-09-23 09:44:43

标签: ruby-on-rails rspec

我有一个it块,其中包含以下内容

it 'creates a new post' do
  post = FactoryGirl.create(:post)
end

it 'accesses a post' do
  expect(Post.count).to eq(1)
end

我没有使用数据库清理程序。在每个测试用例后,post会自动销毁吗?

1 个答案:

答案 0 :(得分:1)

如果你正在使用默认设置,那么你可能在rails_helper.rb中有这个:

config.use_transactional_fixtures = true

这会将每个测试包装在数据库事务中,以便一个测试中的更改不会影响另一个测试。所以,回答你的问题:是的,使用你的代码,第一个测试中的帖子将不会存在于第二个测试中。

如果您希望在post内为所有测试都存在context,则应将此行添加到context

let!(:post) { FactoryGirl.create(:post) }

或者,如果您不需要访问post,只需在数据库中使用它:

before do
  FactoryGirl.create(:post)
end