database_cleaner
database_cleaner copied to clipboard
Add cleaning_with method
Hi,
I'm slowly transitioning my tests to use transaction based cleaning instead of truncation. To do this, I'm currently using an around method which will use the strategy I want only for that test;
around(:each) { |example| DatabaseCleaner.cleaning_with(:transaction, &example) }
This is my extension:
module DatabaseCleaner
class Base
def cleaning_with(*args, &block)
create_strategy(*args).cleaning(&block)
end
end
class << self
def cleaning_with(*args, &inner_block)
connections.reduce(inner_block) do |curr_block, connection|
proc { connection.cleaning_with(*args, &curr_block) }
end.call
end
end
end
Would you consider adding this to the project itself? Btw, I realise doing this introduces some code repetition, I haven't yet looked at that because I didn't want to touch the original source code yet ;-)