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

Defined Under Namespace

Modules: BlockAliases

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_a_kind_of, #be_an_instance_of, #be_close, #change, clear_generated_description, #eql, #equal, #exist, generated_description, #have, #have_at_least, #have_at_most, #include, #match, #method_missing, #raise_exception, #respond_to, #satisfy, #simple_matcher, #throw_symbol, #wrap_expectation

Methods included from Matchers::DSL

#create, #define

Methods included from Subject::ExampleMethods

#__should_for_example_group__, #__should_not_for_example_group__, #should, #should_not, #subject

Dynamic Method Handling

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

Instance Method Details

#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"


20
21
22
23
24
25
26
# File 'lib/spec/example/example_methods.rb', line 20

def description
  if description = @_proxy.description || ::Spec::Matchers.generated_description
    description
  else
    Spec.warn Spec::Example::NoDescriptionError.message("example", @_proxy.location)
  end
end

#eval_each_fail_fast(blocks) ⇒ Object

:nodoc:



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

def eval_each_fail_fast(blocks) # :nodoc:
  blocks.each {|block| instance_eval(&block)}
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(run_options, instance_variables) ⇒ Object

:nodoc:



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
# File 'lib/spec/example/example_methods.rb', line 32

def execute(run_options, instance_variables) # :nodoc:
  run_options.reporter.example_started(@_proxy)
  set_instance_variables_from_hash(instance_variables)

  execution_error = nil
  Timeout.timeout(run_options.timeout) do
    begin
      before_each_example
      instance_eval(&@_implementation)
    rescue Interrupt
      exit 1
    rescue Exception => e
      execution_error ||= e
    end
    begin
      after_each_example
    rescue Interrupt
      exit 1
    rescue Exception => e
      execution_error ||= e
    end
  end

  run_options.reporter.example_finished(@_proxy.update(description), execution_error)
  success = execution_error.nil? || ExamplePendingError === execution_error
end

#expect(&block) ⇒ Object

Extends the submitted block with aliases to and to_not for should and should_not. Allows expectations like this:

expect { this_block }.to change{this.expression}.from(old_value).to(new_value)
expect { this_block }.to raise_error


69
70
71
# File 'lib/spec/example/example_methods.rb', line 69

def expect(&block)
  block.extend BlockAliases
end

#initialize(example_proxy, &implementation) ⇒ Object



115
116
117
118
119
# File 'lib/spec/example/example_methods.rb', line 115

def initialize(example_proxy, &implementation)
  @_proxy = example_proxy
  @_implementation = implementation
  @_backtrace = caller
end

#instance_variable_hashObject

:nodoc:



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

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:



28
29
30
# File 'lib/spec/example/example_methods.rb', line 28

def options # :nodoc:
  @_proxy.options
end

#run_after_eachObject

Run all the after(:each) blocks for this example



111
112
113
# File 'lib/spec/example/example_methods.rb', line 111

def run_after_each
  example_group_hierarchy.run_after_each(self)
end

#run_before_eachObject

Run all the before(:each) blocks for this example



106
107
108
# File 'lib/spec/example/example_methods.rb', line 106

def run_before_each
  example_group_hierarchy.run_before_each(self)
end

#set_instance_variables_from_hash(ivars) ⇒ Object

:nodoc:



96
97
98
99
100
101
102
103
# File 'lib/spec/example/example_methods.rb', line 96

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 ['@_proxy', '@_implementation', '@method_name'].include?(variable_name.to_s)
      instance_variable_set variable_name, value
    end
  end
end

#violated(message = "") ⇒ Object



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

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