Rails Rspec Capybara和DatabaseCleaner - 仅在功能测试中使用截断。

6

我看到了一种只使用数据库清理器:在使用 :js => true 的 capybara 测试中使用截断方法的好方法。

在 spec_helper.rb 中:

config.before(:each) do
  DatabaseCleaner.strategy = if example.metadata[:js]
    :truncation
  else
    :transaction
  end
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end 

问题在于使用capybara进行的任何功能测试似乎都需要清理策略为:截断。

然而,所有其他规范都可以使用较快的:事务。

有没有一种方法只针对capybara功能测试指定策略?类似于:

DataCleaner.strategy( :truncation ) if :type => :feature

1
我不需要被接受的答案,但是你的问题对我非常有用!我正在使用“如果JS使用截断”块,它完美地工作着!谢谢! - Alan LaMielle
1
很高兴能帮忙。我花费在这上面的时间不是很愉快。 - Squadrons
1个回答

3
这应该可以解决,让我知道。
config.after(:all, :type => :feature) do
  DatabaseCleaner.clean_with :truncation
end

你不想在每次测试运行后清理数据库吗? - rubiii
我尝试使用config.before(:suite, :type => :feature),但是没有成功,DatabaseCleaner.strategy = :truncation总是在运行 :/ - Alter Lagos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接