Skip to content
Browse files

tweaks to work with rspec edge and insert fixtures into the db before…

… tests

git-svn-id: http://ar-code.svn.engineyard.com/plugins/model_stubbing@65 d98f8484-0aa7-43af-80ea-302c351499d6
  • Loading branch information...
1 parent e2fa821 commit f1199fd6aacfe883117b6c1703c57550b294912a rick committed Nov 12, 2007
Showing with 107 additions and 49 deletions.
  1. +4 −11 lib/model_stubbing.rb
  2. +14 −1 lib/model_stubbing/definition.rb
  3. +15 −0 lib/model_stubbing/model.rb
  4. +73 −33 lib/model_stubbing/stub.rb
  5. +1 −4 spec/models.rb
View
15 lib/model_stubbing.rb
@@ -14,17 +14,7 @@ def self.definitions() @definitions ||= {} end
# no name is given, it defaults to the current class or :default. Multiple
# #define_models calls with the same name will modify the definition.
def define_models(name = nil, &block)
- if is_a? Class
- name ||= self
- if defined?(Test::Unit::TestCase) && !ancestors.include?(TestUnitExtension) && ancestors.include?(Test::Unit::TestCase)
- self.send :include, TestUnitExtension
- elsif defined?(Spec::DSL::Example) && !ancestors.include?(RspecExtension) && ancestors.include?(Spec::DSL::Example)
- self.send :include, RspecExtension
- end
- else
- name ||= :default
- end
-
+ name ||= is_a?(Class) ? self : :default
defn = ModelStubbing.definitions[name] ||= ModelStubbing::Definition.new
defn.instance_eval(&block)
defn.setup_on self
@@ -56,6 +46,9 @@ def self.guess_mock_framework!
# Included into the current rspec example when #define_models is called.
module RspecExtension
def self.included(base)
+ base.prepend_before :all do
+ self.class.definition.models.values.each &:insert if self.class.definition.database?
+ end
base.prepend_before do
ModelStubbing.stub_current_time_with(current_time) if current_time
end
View
15 lib/model_stubbing/definition.rb
@@ -15,7 +15,9 @@ def time(*args)
# Creates a new ModelStubbing::Model to hold one or more stubs. Multiple calls will append
# any added stubs to the same model instance.
def model(model_name, options = {}, &block)
- (@models[model_name] ||= Model.new(self, model_name, options)).instance_eval(&block)
+ @models[model_name] ||= Model.new(self, model_name, options)
+ @models[model_name].instance_eval(&block)
+ @models[model_name]
end
def initialize(&block)
@@ -37,6 +39,13 @@ def initialize(&block)
def setup_on(klass)
unless klass.respond_to?(:definition) && klass.definition
klass.class_eval do
+ if klass.is_a?(Class)
+ if defined?(Spec::DSL::ExampleGroup) && !klass.ancestors.include?(RspecExtension) && klass.ancestors.include?(Spec::DSL::ExampleGroup)
+ include RspecExtension
+ elsif defined?(Test::Unit::TestCase) && !klass.ancestors.include?(TestUnitExtension) && klass.ancestors.include?(Test::Unit::TestCase)
+ include TestUnitExtension
+ end
+ end
def stubs(key, attributes = {})
self.class.definition.retrieve_record(key, attributes)
end
@@ -59,6 +68,10 @@ def retrieve_record(key, attributes = {})
@stubs[key].record(attributes)
end
+ def database?
+ Object.const_defined?(ActiveRecord)
+ end
+
def inspect
"(ModelStubbing::Definition(:models => [#{@models.keys.collect { |k| k.to_s }.sort.join(", ")}]))"
end
View
15 lib/model_stubbing/model.rb
@@ -74,5 +74,20 @@ def stub_method_definition
def inspect
"(ModelStubbing::Model(#{@name.inspect} => [#{@stubs.keys.collect { |k| k.to_s }.sort.join(", ")}]))"
end
+
+ def insert
+ model_class.transaction do
+ purge
+ @stubs.values.each &:insert
+ end
+ end
+
+ def purge
+ model_class.connection.execute "TRUNCATE TABLE #{connection.quote_column_name model_class.table_name}"
+ end
+
+ def connection
+ @connection ||= model_class.connection
+ end
end
end
View
106 lib/model_stubbing/stub.rb
@@ -34,43 +34,83 @@ def inspect
"(ModelStubbing::Stub(#{@name.inspect} => #{attributes.inspect}))"
end
- private
- def instantiate(attributes)
- stubs = {}
- default_record = attributes.empty?
-
- @attributes.each do |key, value|
- stubs[key] = value if value.is_a?(Stub)
- end
-
- attributes.each do |key, value|
- case value
- when Stub
- stubs[key] = attributes.delete(key)
- when Hash
- stubs[key] = stubs[key].record(value)
- end
- end
-
- attributes = @attributes.merge(attributes)
+ def insert(attributes = {})
+ object = record(attributes)
+ connection.insert_fixture(object.stubbed_attributes, model.model_class.table_name)
+ end
+
+ def connection
+ @connection ||= @model.connection
+ end
+
+ private
+ def instantiate(attributes)
+ default_record = attributes.empty?
+ stubs, attributes = stubbed_attributes(attributes)
- record = @model.model_class.new(attributes)
- meta = class << record ; self ; end
-
- meta.send :attr_accessor, :id
- record.id = @model.model_class.mock_id
+ record = @model.model_class.new(attributes)
+ meta = class << record
+ def new_record?() false end
+ self
+ end
+
+ meta.send :attr_accessor, :stubbed_attributes
+ record.id = @model.model_class.mock_id
+ record.stubbed_attributes = attributes.merge(:id => record.id)
- stubs.each do |key, value|
- meta.send :attr_accessor, key
- record.send("#{key}=", value.is_a?(Stub) ? value.record : value)
- end
-
- @model.records[self] = record if default_record
- record
+ stubs.each do |key, value|
+ meta.send :attr_accessor, key
+ record.send("#{key}=", value.is_a?(Stub) ? value.record : value)
end
- def retrieve
- @model.records[self]
+ @model.records[self] = record if default_record
+ record
+ end
+
+ def stubbed_attributes(attributes)
+ stubs = {}
+ stubbed = FixtureHash.new(self)
+
+ @attributes.each do |key, value|
+ stubbed[key] = value
+ stubs[key] = value if value.is_a?(Stub)
end
+
+ attributes.each do |key, value|
+ case value
+ when Stub
+ stubs[key] = attributes.delete(key)
+ when Hash
+ stubs[key] = stubs[key].record(value)
+ end
+ end
+
+ [stubs, stubbed.update(attributes)]
+ end
+
+ def retrieve
+ @model.records[self]
+ end
+ end
+
+ class FixtureHash < Hash
+ def initialize(stub)
+ super()
+ @stub = stub
+ end
+
+ def key_list
+ keys.collect { |column_name| @stub.connection.quote_column_name(column_name) } * ", "
+ end
+
+ def value_list
+ klass = @stub.model.model_class
+
+ list = inject([]) do |fixtures, (key, value)|
+ col = klass.columns_hash[key] if klass.ancestors.include?(ActiveRecord::Base)
+ fixtures << @stub.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
+ end
+ list * ', '
+ end
end
end
View
5 spec/models.rb
@@ -12,12 +12,9 @@ class FakeTester
end
class BlankModel
+ attr_accessor :id
attr_reader :attributes
- def id
- nil
- end
-
def initialize(attributes = {})
@attributes = attributes
attributes.each do |key, value|

0 comments on commit f1199fd

Please sign in to comment.
Something went wrong with that request. Please try again.