2010-06-10 13:37:51 -04:00
|
|
|
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
2009-04-11 11:27:23 -04:00
|
|
|
|
|
|
|
require 'rubygems'
|
2010-11-11 16:33:45 -05:00
|
|
|
require 'rspec'
|
|
|
|
require 'rspec/autorun'
|
2009-04-11 11:27:23 -04:00
|
|
|
require 'rr'
|
|
|
|
|
|
|
|
require 'factory_girl'
|
|
|
|
|
2010-11-11 16:33:45 -05:00
|
|
|
module RR
|
|
|
|
module Adapters
|
|
|
|
module Rspec
|
|
|
|
def self.included(mod)
|
|
|
|
RSpec.configuration.backtrace_clean_patterns.push(RR::Errors::BACKTRACE_IDENTIFIER)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.mock_framework = :rr
|
|
|
|
RSpec::Core::ExampleGroup.send(:include, RR::Adapters::Rspec)
|
2010-06-10 14:58:47 -04:00
|
|
|
config.after do
|
2010-06-24 09:45:57 -04:00
|
|
|
FactoryGirl.factories.clear
|
|
|
|
FactoryGirl.sequences.clear
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-06-10 13:37:51 -04:00
|
|
|
|
2010-11-11 16:33:45 -05:00
|
|
|
module DefinesConstants
|
|
|
|
def self.included(example_group)
|
|
|
|
example_group.class_eval do
|
|
|
|
before do
|
|
|
|
@defined_constants = []
|
2010-11-12 15:58:25 -05:00
|
|
|
@created_tables = []
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-10 13:37:51 -04:00
|
|
|
|
2010-11-11 16:33:45 -05:00
|
|
|
after do
|
|
|
|
@defined_constants.reverse.each do |path|
|
|
|
|
namespace, class_name = *constant_path(path)
|
|
|
|
namespace.send(:remove_const, class_name)
|
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
@created_tables.each do |table_name|
|
|
|
|
ActiveRecord::Base.
|
|
|
|
connection.
|
|
|
|
execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
end
|
2010-11-11 16:33:45 -05:00
|
|
|
end
|
2010-06-10 13:37:51 -04:00
|
|
|
|
2010-11-11 16:33:45 -05:00
|
|
|
def define_class(path, base = Object, &block)
|
|
|
|
namespace, class_name = *constant_path(path)
|
|
|
|
klass = Class.new(base)
|
|
|
|
namespace.const_set(class_name, klass)
|
|
|
|
klass.class_eval(&block) if block_given?
|
|
|
|
@defined_constants << path
|
|
|
|
klass
|
|
|
|
end
|
2010-06-10 13:37:51 -04:00
|
|
|
|
2010-11-12 15:58:25 -05:00
|
|
|
def define_model(name, columns = {}, &block)
|
|
|
|
model = define_class(name, ActiveRecord::Base, &block)
|
|
|
|
create_table(model.table_name) do |table|
|
|
|
|
columns.each do |name, type|
|
|
|
|
table.column name, type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
model
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_table(table_name, &block)
|
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
|
|
|
|
begin
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
connection.create_table(table_name, &block)
|
|
|
|
@created_tables << table_name
|
|
|
|
connection
|
|
|
|
rescue Exception => exception
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
raise exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-11 16:33:45 -05:00
|
|
|
def constant_path(constant_name)
|
|
|
|
names = constant_name.split('::')
|
|
|
|
class_name = names.pop
|
|
|
|
namespace = names.inject(Object) { |result, name| result.const_get(name) }
|
|
|
|
[namespace, class_name]
|
|
|
|
end
|
|
|
|
end
|
2010-06-10 13:37:51 -04:00
|
|
|
end
|
|
|
|
end
|
2010-11-11 16:33:45 -05:00
|
|
|
|