Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 10, 2009
Darrick Wiebe Allow a recursive search up the class heirarchy for the definition of…
… the models, allowing the stub method to be called from nested describe blocks in rspec.
7f0f331
Darrick Wiebe Bettor support for nested definitions.
The database rollback needs to happen even if no models were initiated because they could always be manually created. Allow Definition instances to know their own name. Improve upon the previous method of searching the class heirarchy for definitions, now search for what was defined and then redefine it in the current subclass -- that actually got all of my tests passing in my app.
1580516
Darrick Wiebe Correctly initialize the first test after dynamically calling define_…
…models.
e5dff0b
View
2  lib/model_stubbing.rb
@@ -36,7 +36,7 @@ def self.define_models(name = nil, options = {}, &block)
name ||= is_a?(Class) ? self : :default
base_name = options[:copy] || :default
base = name == base_name ? nil : ModelStubbing.definitions[base_name]
- defn = ModelStubbing.definitions[name] ||= (base && options[:copy] != false) ? base.dup : ModelStubbing::Definition.new
+ defn = ModelStubbing.definitions[name] ||= (base && options[:copy] != false) ? base.dup : ModelStubbing::Definition.new(name)
options = base.options.merge(options) if base
defn.setup_on options[:target] || self, options, &block
end
View
13 lib/model_stubbing/definition.rb
@@ -4,7 +4,7 @@ module ModelStubbing
# rspec example.
class Definition
attr_writer :insert, :current_time
- attr_reader :models, :stubs, :ordered_models, :options
+ attr_reader :models, :stubs, :ordered_models, :options, :name
# Sets the time that Time.now is mocked to (in UTC)
def time(*args)
@@ -33,7 +33,8 @@ def model(klass, options = {}, &block)
@models[m.name]
end
- def initialize(&block)
+ def initialize(name = nil, &block)
+ @name = name
@ordered_models = []
@models = {}
@stubs = {}
@@ -114,17 +115,11 @@ def teardown!
def setup_test_run
ModelStubbing.records.clear
ModelStubbing.stub_current_time_with(current_time) if current_time
- return unless database?
- ActiveRecord::Base.connection.increment_open_transactions
- ActiveRecord::Base.connection.begin_db_transaction
end
def teardown_test_run
ModelStubbing.records.clear
# TODO: teardown Time.stubs(:now)
- return unless database?
- ActiveRecord::Base.connection.rollback_db_transaction
- ActiveRecord::Base.verify_active_connections!
end
def database?
@@ -135,4 +130,4 @@ def inspect
"(ModelStubbing::Definition(:models => [#{@models.keys.collect { |k| k.to_s }.sort.join(", ")}]))"
end
end
-end
+end
View
29 lib/model_stubbing/extensions.rb
@@ -23,18 +23,33 @@ def create_model_methods_for(models)
end
end
+ def database?
+ defined?(ActiveRecord)
+ end
+
def setup_fixtures
ModelStubbing.records.clear
- return unless self.class.definition
- unless self.class.definition_inserted
- self.class.definition.insert!
- self.class.definition_inserted = true
+ if self.class.definition
+ unless self.class.definition_inserted
+ self.class.definition.insert!
+ self.class.definition_inserted = true
+ end
+ self.class.definition.setup_test_run
+ end
+ if database?
+ ActiveRecord::Base.connection.increment_open_transactions
+ ActiveRecord::Base.connection.begin_db_transaction
end
- self.class.definition.setup_test_run
end
def teardown_fixtures
- self.class.definition && self.class.definition.teardown_test_run
+ if self.class.definition
+ self.class.definition.teardown_test_run
+ end
+ if database?
+ ActiveRecord::Base.connection.rollback_db_transaction
+ ActiveRecord::Base.verify_active_connections!
+ end
end
def stubs(key)
@@ -45,4 +60,4 @@ def current_time
self.class.definition && self.class.definition.current_time
end
end
-end
+end
View
49 lib/model_stubbing/model.rb
@@ -35,7 +35,7 @@ class << @model_class
end
instance_eval &block if block
end
-
+
def default_name
name = @model_class.name
if name.respond_to?(:underscore)
@@ -82,10 +82,47 @@ def retrieve_record(key, attributes = {})
end
def stub_method_definition
- "def #{@plural}(key, attrs = {}) self.class.definition.models[#{@plural.inspect}].retrieve_record(key, attrs) end\n
- def new_#{@singular}(key = :default, attrs = {}) key, attrs = :default, key if key.is_a?(Hash) ; #{@plural}(key, attrs.merge(:id => :new)) end\n
- def new_#{@singular}!(key = :default, attrs = {}) key, attrs = :default, key if key.is_a?(Hash) ; #{@plural}(key, attrs.merge(:id => :dup)) end\n
- def create_#{@singular}(key = :default, attrs = {}) stub = new_#{@singular}(key, attrs) ; stub.save! ; stub end"
+ <<-END
+ def #{@plural}(key, attrs = {})
+ klass = self.class
+ unless defined?(klass.definition) and klass.definition
+ # If we are in a subclass where define_models was called on a superclass but
+ # not on this subclass (e.g. in a nested define block) then define_models
+ # needs to be called again for this subclass in order for the teardown to
+ # happen correctly.
+ k = klass.superclass
+ name = nil
+ until k == Object or k.nil?
+ if defined?(k.definition) and k.definition
+ name = k.definition.name
+ k = nil
+ end
+ end
+ klass.module_eval { define_models name }
+ unless klass.definition_inserted
+ klass.definition.insert!
+ # Don't want to set definition_inserted to true because it will
+ # roll back at the end of the first test. The next test will
+ # correctly insert again before the transaction begins.
+ end
+ klass.definition.setup_test_run
+ end
+ klass.definition.models[#{@plural.inspect}].retrieve_record(key, attrs)
+ end
+ def new_#{@singular}(key = :default, attrs = {})
+ key, attrs = :default, key if key.is_a?(Hash)
+ #{@plural}(key, attrs.merge(:id => :new))
+ end
+ def new_#{@singular}!(key = :default, attrs = {})
+ key, attrs = :default, key if key.is_a?(Hash)
+ #{@plural}(key, attrs.merge(:id => :dup))
+ end
+ def create_#{@singular}(key = :default, attrs = {})
+ stub = new_#{@singular}(key, attrs)
+ stub.save!
+ stub
+ end
+ END
end
def inspect
@@ -125,4 +162,4 @@ def method_missing(model_name, stub_name, *args)
end
end
end
-end
+end

No commit comments for this range

Something went wrong with that request. Please try again.