Module: Speccify::ExampleGroupClassMethods

Defined in:
lib/speccify.rb

Overview

These method extend the ExampleGroup as class methods. The methods defined in this module are available inside the example groups (‘describe’ blocks).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descObject

Returns the value of attribute desc.



14
15
16
# File 'lib/speccify.rb', line 14

def desc
  @desc
end

#setup_chainedObject

:nodoc:



17
18
19
# File 'lib/speccify.rb', line 17

def setup_chained
  @setup_chained
end

#teardown_chainedObject

:nodoc:



21
22
23
# File 'lib/speccify.rb', line 21

def teardown_chained
  @teardown_chained
end

Instance Method Details

#after(type = :each, &block) ⇒ Object

Run after each Test.

The code in the block attached to this method will be run after each example in all subsequent, eventually nested example groups.



38
39
40
41
42
43
# File 'lib/speccify.rb', line 38

def after(type = :each, &block)
  raise "unsupported after type: #{type}" unless type == :each
  passed_through_teardown = self.teardown_chained
  self.teardown_chained = lambda {instance_eval(&block);instance_eval(&passed_through_teardown) }
  define_method :teardown, &self.teardown_chained
end

#before(type = :each, &block) ⇒ Object

Run before each Test.

The code in the block attached to this method will be run before each example in all subsequent, eventually nested example groups.



28
29
30
31
32
33
# File 'lib/speccify.rb', line 28

def before(type = :each, &block)
  raise "unsupported before type: #{type}" unless type == :each
  passed_through_setup = self.setup_chained
  self.setup_chained = lambda { instance_eval(&passed_through_setup);instance_eval(&block) }
  define_method :setup, &self.setup_chained
end

#describe(desc, &block) ⇒ Object

Define an Example Group.

Alike TestCase.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/speccify.rb', line 47

def describe desc, &block
  modules = self.ancestors
  cls = Class.new(self.superclass) do |klass|
    modules.each {|mod| include mod if (mod.class.to_s == "Module" && !(self < mod))}
  end
  Object.const_set self.name + desc.to_s.split(/\W+/).map { |s| s.capitalize }.join, cls
  cls.setup_chained = self.setup_chained
  cls.teardown_chained = self.teardown_chained
  cls.desc = self.desc + " " + desc
  cls.tests($1.constantize) if defined?(Rails) && self.name =~ /^(.*(Controller|Helper|Mailer))Test/
  cls.class_eval(&block)
end

#it(desc, &block) ⇒ Object

Define a test.

Alike ‘def test_whatever ..’ test definition.



62
63
64
65
66
67
# File 'lib/speccify.rb', line 62

def it desc, &block
  self.before {}
  desc = Speccify::Functions.make_constantizeable(desc)
  define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &lambda {$current_spec = self; instance_eval(&block)} if block_given?
  self.after {}
end