Class: Shoulda::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_background/shoulda.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fixture_backgroundObject (readonly)

Returns the value of attribute fixture_background.



11
12
13
# File 'lib/fixture_background/shoulda.rb', line 11

def fixture_background
  @fixture_background
end

Instance Method Details

#background(&blk) ⇒ Object



23
24
25
# File 'lib/fixture_background/shoulda.rb', line 23

def background(&blk)
  @fixture_background = FixtureBackground::Background.new(full_class_name, test_unit_class, parent_fixture_background, blk)
end

#buildObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fixture_background/shoulda.rb', line 58

def build
  klass = class_for_test
  
  shoulds.each do |should|
    create_test_from_should_hash(klass, should)
  end

  subcontexts.each { |context| context.build }

  print_should_eventuallys
end

#class_for_testObject



27
28
29
30
31
# File 'lib/fixture_background/shoulda.rb', line 27

def class_for_test
  (@fixture_background && @fixture_background.class_for_test) || 
  (parent_fixture_background && FixtureBackground::Background.class_for_test(full_class_name, parent_fixture_background, test_unit_class)) || 
  test_unit_class
end

#create_test_from_should_hash(klass, should) ⇒ Object

the following functions are copied from shoulda/context.rb



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fixture_background/shoulda.rb', line 37

def create_test_from_should_hash(klass, should)
  test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym

  if klass.instance_methods.include?(test_name.to_s)
    warn "  * WARNING: '#{test_name}' is already defined"
  end

  context = self
  klass.send(:define_method, test_name) do
    @shoulda_context = context
    begin
      context.run_parent_setup_blocks(self)
      should[:before].bind(self).call if should[:before]
      context.run_current_setup_blocks(self)
      should[:block].bind(self).call
    ensure
      context.run_all_teardown_blocks(self)
    end
  end
end

#full_class_nameObject



13
14
15
# File 'lib/fixture_background/shoulda.rb', line 13

def full_class_name
  (test_unit_class.name + full_name.gsub(/[^a-zA-Z0-9]/, '_')).camelcase
end

#parent_fixture_backgroundObject



17
18
19
20
21
# File 'lib/fixture_background/shoulda.rb', line 17

def parent_fixture_background
  if parent && parent.respond_to?(:fixture_background)
    parent.fixture_background || parent.parent_fixture_background
  end
end