Class: Crspec::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, metadata, example_group, block) ⇒ Example

Returns a new instance of Example.



9
10
11
12
13
14
15
16
17
18
# File 'lib/crspec/example.rb', line 9

def initialize(description, , example_group, block)
  @id = SecureRandom.uuid
  @description = description
  @metadata = .freeze
  @example_group = example_group
  @block = block
  @status = :pending
  @error = nil
  @execution_time = 0
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/crspec/example.rb', line 7

def block
  @block
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/crspec/example.rb', line 7

def description
  @description
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/crspec/example.rb', line 7

def error
  @error
end

#example_groupObject (readonly)

Returns the value of attribute example_group.



7
8
9
# File 'lib/crspec/example.rb', line 7

def example_group
  @example_group
end

#execution_timeObject (readonly)

Returns the value of attribute execution_time.



7
8
9
# File 'lib/crspec/example.rb', line 7

def execution_time
  @execution_time
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/crspec/example.rb', line 7

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/crspec/example.rb', line 7

def 
  @metadata
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/crspec/example.rb', line 7

def status
  @status
end

Instance Method Details

#execute!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/crspec/example.rb', line 20

def execute!
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  instance = @example_group.create_instance(self)

  begin
    run_hooks(instance, :before)
    instance.instance_exec(&@block) if @block
    @status = :passed
  rescue StandardError, ExpectationNotMetError => e
    @status = :failed
    @error = e
  ensure
    begin
      run_hooks(instance, :after)
    rescue StandardError => e
      if @status == :passed
        @status = :failed
        @error = e
      end
    end
    @execution_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  end
end