Module: Spec::Example::ExampleMethods

Extended by:
ModuleReopeningFix
Includes:
Pending, Subject::ExampleMethods, Matchers
Included in:
ExampleGroup, Test::Unit::TestCase
Defined in:
lib/spec/example/example_methods.rb

Instance Method Summary collapse

Methods included from ModuleReopeningFix

child_modules, include, included

Methods included from Pending

#pending

Methods included from Matchers

#be, #be_a, #be_close, #change, clear_generated_description, #eql, #equal, #exist, generated_description, #has, #have, #have_at_least, #have_at_most, #include, last_matcher, last_matcher=, last_should, last_should=, #match, #method_missing, #raise_error, #respond_to, #satisfy, #simple_matcher, #throw_symbol, #wrap_expectation

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spec::Matchers

Instance Method Details

#backtraceObject

Provides the backtrace up to where this example was declared.



103
104
105
# File 'lib/spec/example/example_methods.rb', line 103

def backtrace
  @_backtrace
end

#descriptionObject

Declared description for this example:

describe Account do
  it "should start with a balance of 0" do
  ...

description
=> "should start with a balance of 0"


21
22
23
# File 'lib/spec/example/example_methods.rb', line 21

def description
  @_defined_description || ::Spec::Matchers.generated_description || "NO NAME"
end

#eval_blockObject

:nodoc:



98
99
100
# File 'lib/spec/example/example_methods.rb', line 98

def eval_block # :nodoc:
  instance_eval(&@_implementation)
end

#eval_each_fail_fast(blocks) ⇒ Object

:nodoc:



71
72
73
74
75
# File 'lib/spec/example/example_methods.rb', line 71

def eval_each_fail_fast(blocks) # :nodoc:
  blocks.each do |block|
    instance_eval(&block)
  end
end

#eval_each_fail_slow(blocks) ⇒ Object

:nodoc:



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/spec/example/example_methods.rb', line 77

def eval_each_fail_slow(blocks) # :nodoc:
  first_exception = nil
  blocks.each do |block|
    begin
      instance_eval(&block)
    rescue Exception => e
      first_exception ||= e
    end
  end
  raise first_exception if first_exception
end

#execute(options, instance_variables) ⇒ Object

:nodoc:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spec/example/example_methods.rb', line 41

def execute(options, instance_variables) # :nodoc:
  options.reporter.example_started(self)
  set_instance_variables_from_hash(instance_variables)
  
  execution_error = nil
  Timeout.timeout(options.timeout) do
    begin
      before_each_example
      eval_block
    rescue Exception => e
      execution_error ||= e
    end
    begin
      after_each_example
    rescue Exception => e
      execution_error ||= e
    end
  end

  options.reporter.example_finished(self, execution_error)
  success = execution_error.nil? || ExamplePendingError === execution_error
end

#full_descriptionObject

Concats the class description with the example description.

describe Account do
  it "should start with a balance of 0" do
  ...

full_description
=> "Account should start with a balance of 0"


33
34
35
# File 'lib/spec/example/example_methods.rb', line 33

def full_description
  "#{self.class.description} #{self.description}"
end

#implementation_backtraceObject

Deprecated - use backtrace()



108
109
110
111
112
113
114
# File 'lib/spec/example/example_methods.rb', line 108

def implementation_backtrace
  Kernel.warn <<-WARNING
ExampleMethods#implementation_backtrace is deprecated and will be removed
from a future version. Please use ExampleMethods#backtrace instead.
WARNING
  backtrace
end

#instance_variable_hashObject

:nodoc:



64
65
66
67
68
69
# File 'lib/spec/example/example_methods.rb', line 64

def instance_variable_hash # :nodoc:
  instance_variables.inject({}) do |variable_hash, variable_name|
    variable_hash[variable_name] = instance_variable_get(variable_name)
    variable_hash
  end
end

#optionsObject

:nodoc:



37
38
39
# File 'lib/spec/example/example_methods.rb', line 37

def options # :nodoc:
  @_options
end

#set_instance_variables_from_hash(ivars) ⇒ Object

:nodoc:



89
90
91
92
93
94
95
96
# File 'lib/spec/example/example_methods.rb', line 89

def set_instance_variables_from_hash(ivars) # :nodoc:
  ivars.each do |variable_name, value|
    # Ruby 1.9 requires variable.to_s on the next line
    unless ['@_implementation', '@_defined_description', '@_matcher_description', '@method_name'].include?(variable_name.to_s)
      instance_variable_set variable_name, value
    end
  end
end

#violated(message = "") ⇒ Object



9
10
11
# File 'lib/spec/example/example_methods.rb', line 9

def violated(message="")
  raise Spec::Expectations::ExpectationNotMetError.new(message)
end