Class: RSpec::Core::Example

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/rspec/fixes/rspec/core/example.rb,
opal/opal/rspec/async/async_example.rb

Direct Known Subclasses

Opal::RSpec::LegacyAsyncExample

Defined Under Namespace

Classes: ExecutionResult

Instance Method Summary collapse

Instance Method Details

#assign_generated_descriptionObject

more mutable strings



3
4
5
6
7
8
9
10
11
12
# File 'opal/opal/rspec/fixes/rspec/core/example.rb', line 3

def assign_generated_description
  if [:description].empty? && (description = RSpec::Matchers.generated_description)
    [:description] = description
    [:full_description] = [:full_description] + description # mutable string fix
  end
rescue Exception => e
  set_exception(e, "while assigning the example description")
ensure
  RSpec::Matchers.clear_generated_description
end

#core_block_runObject



2
3
4
5
6
7
8
9
10
11
# File 'opal/opal/rspec/async/async_example.rb', line 2

def core_block_run
  example_promise = Promise.value(@example_group_instance.instance_exec(self, &@example_block))
  example_promise.then do |result|
    result
  end.rescue do |ex|
    ex ||= Exception.new 'Async promise failed for unspecified reason'
    ex = Exception.new ex unless ex.kind_of?(Exception)
    ex
  end
end

#resolve_subjectObject

TODO: Use subject! to create a before hook on the fly, might be cleaner than this Might be a better way to do this, but then you end up with promises around the expectation handlers, which could get ugly



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'opal/opal/rspec/async/async_example.rb', line 15

def resolve_subject
  begin
    subj = example_group_instance.subject
    if subj.is_a? Promise
      return subj.then do |resolved_subject|
        # This is a private method, but we're using Opal
        example_group_instance.__memoized[:subject] = resolved_subject
      end
    end
  rescue Exception => _ # Can't use empty rescue in Opal 0.10 because it won't catch native JS exceptions (which aren't StandardError instances)
    # Exception occurred while checking the subject, might be that the example group had a described class, was not intending on using it as the subject,
    # and the initializer for that described class failed
  end
  Promise.value(true)
end

#run(example_group_instance, reporter) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'opal/opal/rspec/async/async_example.rb', line 42

def run(example_group_instance, reporter)
  @example_group_instance = example_group_instance
  RSpec.current_example = self

  start(reporter)
  Pending.mark_pending!(self, pending) if pending?

  Promise.value(true).then do
    Promise.value(true).then do
      if skipped?
        Pending.mark_pending! self, skip
      elsif !RSpec.configuration.dry_run?
        with_around_example_hooks do
          Promise.value(true).then do
            run_before_example.then do
              resolve_subject
            end.then do
              core_block_run
            end.then do
              if pending?
                Pending.mark_fixed! self

                raise Pending::PendingExampleFixedError,
                      'Expected example to fail since it is pending, but it passed.',
                      [location]
              end
            end
          end.rescue do |e|
            # no-op, required metadata has already been set by the `skip`
            # method.
            unless e.is_a? Pending::SkipDeclaredInExample
              set_exception(e)
            end
          end.ensure do
            run_after_example
          end
        end
      end
    end.rescue do |e|
      set_exception(e)
    end.ensure do
      @example_group_instance.instance_variables.each do |ivar|
        @example_group_instance.instance_variable_set(ivar, nil)
      end
      @example_group_instance = nil
    end
  end.then do
    finish(reporter)
  end.ensure do |result|
    RSpec.current_example = nil
    # promise always/ensure do not behave exactly like ensure, need to be explicit about value being returned
    result
  end
end

#run_after_exampleObject



31
32
33
34
35
36
37
38
39
40
# File 'opal/opal/rspec/async/async_example.rb', line 31

def run_after_example
  @example_group_class.hooks.run(:after, :example, self).then do
    verify_mocks
    assign_generated_description if RSpec.configuration.expecting_with_rspec?
  end.rescue do |e|
    set_exception(e, "in an `after(:example)` hook")
  end.ensure do
    @example_group_instance.teardown_mocks_for_rspec
  end
end