Best Factory_bot_ruby code snippet using DefineConstantMacros.created_tables
define_constant.rb
Source:define_constant.rb
...21 connection = ActiveRecord::Base.connection22 begin23 connection.execute("DROP TABLE IF EXISTS #{table_name}")24 connection.create_table(table_name, &block)25 @created_tables << table_name26 connection27 rescue Exception => exception28 connection.execute("DROP TABLE IF EXISTS #{table_name}")29 raise exception30 end31 end32 def constant_path(constant_name)33 names = constant_name.split('::')34 class_name = names.pop35 namespace = names.inject(Object) { |result, name| result.const_get(name) }36 [namespace, class_name]37 end38 def default_constants39 @defined_constants ||= []40 @created_tables ||= []41 end42 def clear_generated_constants43 @defined_constants.reverse.each do |path|44 namespace, class_name = *constant_path(path)45 namespace.send(:remove_const, class_name)46 end47 @defined_constants.clear48 end49 def clear_generated_tables50 @created_tables.each do |table_name|51 ActiveRecord::Base.52 connection.53 execute("DROP TABLE IF EXISTS #{table_name}")54 end55 @created_tables.clear56 end57end58RSpec.configure do |config|59 config.include DefineConstantMacros60 config.before(:all) do61 ActiveRecord::Base.establish_connection(62 adapter: 'sqlite3',63 database: File.join(File.dirname(__FILE__), 'test.db')64 )65 end66 config.before do67 default_constants68 end69 config.after do...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!