Module: Stratagem::AutoMock::Factory

Includes:
ValueGenerator
Included in:
Aquifer
Defined in:
lib/stratagem/auto_mock/factory.rb

Constant Summary

Constants included from ValueGenerator

ValueGenerator::ADDRESSES, ValueGenerator::GENERIC_NAMES, ValueGenerator::GENERIC_URLS, ValueGenerator::NUMERIC_TYPES, ValueGenerator::USERS

Instance Method Summary collapse

Methods included from ValueGenerator

#generate_boolean, #generate_datetime, #generate_float, #generate_int, #generate_string, #generate_text, #generate_value

Instance Method Details

#add_mocked(instance) ⇒ Object



61
62
63
# File 'lib/stratagem/auto_mock/factory.rb', line 61

def add_mocked(instance)
  (mocked(instance.class)) << instance
end

#mock(model, mock_chain = [], belongs_to = nil) ⇒ Object

Note: If a class is passed in that has descendants, a leaf descendant will be chosen at random



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stratagem/auto_mock/factory.rb', line 23

def mock(model,mock_chain=[], belongs_to=nil)
  while (model.stratagem.subclasses?)
    prior_model = model
    model = model.sg_subclasses[rand model.sg_subclasses.size]
    puts "mapped base class #{prior_model.name} -> #{model.name} "
  end
  
  return if mock_chain.select {|m| m == model }.size > 1
  mock_chain << model

  object,valid = model.new, nil
  begin
    10.times do |i|
      log "mocking model #{model.name}"
      populate_attributes(object, mock_chain)
      correct_invalid_columns(object, mock_chain)
      
      valid = populate_relations(object, mock_chain, belongs_to)

      # allow relationship attributes to be invalid
      valid = save_object(object) if (valid)
    
      if valid
        add_mocked(object)
        break
      end
    end
  rescue Exception, ActiveRecord::ActiveRecordError
    e = MockError.new("Unexpected error mocking model #{model.name} - #{$!.message}")
    puts e.message
    e.target = $!
    Stratagem.logger.error(e)
    # raise e
  end

  return [object,valid] if valid
end

#mock_attributes(model) ⇒ Object



14
15
16
17
18
19
# File 'lib/stratagem/auto_mock/factory.rb', line 14

def mock_attributes(model)
  object = model.new
  populate_attributes(object, [])
  correct_invalid_columns(object, [])
  object.stratagem.mock_attributes
end