Skip to content

Instantly share code, notes, and snippets.

@replaid
Last active December 30, 2015 00:38
Show Gist options
  • Save replaid/7750533 to your computer and use it in GitHub Desktop.
Save replaid/7750533 to your computer and use it in GitHub Desktop.
Instrumenting the spec run to catch test data leakage at the source. Use like this: bundle exec rspec --seed _(seed number here)_ file1 file2 file3 ...
...
def check_for_pollution
tables_with_records = []
all_tables = ActiveRecord::Base.connection.tables
all_tables.each do |table_name|
has_records = (ActiveRecord::Base.connection.select_value("select exists (select 1 from #{table_name} limit 1)") > 0)
tables_with_records << table_name if has_records
end
expected_tables_with_records = ['schema_migrations']
unexpected_tables_with_records = tables_with_records - expected_tables_with_records
raise "Tables with post-spec pollution: #{unexpected_tables_with_records.inspect}" unless unexpected_tables_with_records.empty?
unexpected_tables_with_records.should == []
end
require 'spork'
Spork.prefork do
...
RSpec.configure do |config|
# config.formatter = EarlyInfoFormatter # :documentation, :progress, :html, :textmate, AnxiousFormatter, EarlyInfoFormatter
config.formatter = :documentation
...
config.before :each do
...
check_for_pollution
end
end
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment