Permalink
Browse files

ensure that stubs are loaded in the order that they're entered

  • Loading branch information...
1 parent ccc4392 commit 83c168a9d2068729822ae3dae976e8e48504c476 @technoweenie committed Aug 15, 2008
Showing with 16 additions and 9 deletions.
  1. +14 −9 lib/model_stubbing/model.rb
  2. +2 −0 lib/model_stubbing/stub.rb
@@ -2,7 +2,7 @@ module ModelStubbing
# Models hold one or more stubs.
class Model
attr_accessor :name, :plural, :singular
- attr_reader :definition, :stubs, :model_class, :options
+ attr_reader :definition, :stubs, :model_class, :options, :ordered_stubs
# Creates a stub for this model. A stub with no name is assumed to be the default
# stub. A global key for the definition is also created based on the singular
@@ -17,13 +17,14 @@ def stub(name = nil, options = {})
end
def initialize(definition, klass, options = {}, &block)
- @definition = definition
- @model_class = klass
- @name = options.delete(:name) || default_name.to_sym
- @plural = options.delete(:plural) || name
- @singular = options.delete(:singular) || name.to_s.singularize
- @options = options
- @stubs = {}
+ @definition = definition
+ @model_class = klass
+ @name = options.delete(:name) || default_name.to_sym
+ @plural = options.delete(:plural) || name
+ @singular = options.delete(:singular) || name.to_s.singularize
+ @options = options
+ @stubs = {}
+ @ordered_stubs = []
unless @model_class.respond_to?(:mock_id)
class << @model_class
define_method :mock_id do
@@ -92,7 +93,11 @@ def inspect
def insert
purge
- @stubs.values.each &:insert
+ @ordered_stubs.each do |name|
+ if stub = @stubs[name]
+ stub.insert
+ end
+ end
end
def purge
@@ -19,6 +19,8 @@ def initialize(model, name, attributes)
end
@global_key = (name == :default ? @model.singular : "#{name}_#{@model.singular}").to_sym
+ @model.ordered_stubs << name
+ @model.ordered_stubs.uniq!
@model.all_stubs[@global_key] = @model.stubs[name] = self
end

0 comments on commit 83c168a

Please sign in to comment.