Module: EventedSpec::SpecHelper

Includes:
AMQPHelpers::ExampleMethods, CoolioHelpers::ExampleHelpers, EventMachineHelpers::ExampleMethods
Defined in:
lib/evented-spec/spec_helper.rb,
lib/evented-spec/evented_example.rb,
lib/evented-spec/spec_helper/amqp_helpers.rb,
lib/evented-spec/spec_helper/amqp_helpers.rb,
lib/evented-spec/evented_example/em_example.rb,
lib/evented-spec/spec_helper/coolio_helpers.rb,
lib/evented-spec/spec_helper/coolio_helpers.rb,
lib/evented-spec/evented_example/amqp_example.rb,
lib/evented-spec/evented_example/coolio_example.rb,
lib/evented-spec/spec_helper/event_machine_helpers.rb,
lib/evented-spec/spec_helper/event_machine_helpers.rb

Overview

EventedSpec::SpecHelper module defines #ampq and #em methods that can be safely used inside your specs (examples) to test code running inside AMQP.start or EM.run loop respectively. Each example is running in a separate event loop, you can control for timeouts either with :spec_timeout option given to #amqp/#em/#coolio method or setting a default timeout using default_timeout(timeout) macro inside describe/context block.

Defined Under Namespace

Modules: AMQPHelpers, CoolioHelpers, EventMachineHelpers, GroupMethods Classes: AMQPExample, CoolioExample, EMExample, EventedExample

Constant Summary collapse

SpecTimeoutExceededError =

Error which shows in RSpec log when example does not call #done inside of event loop.

Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventMachineHelpers::ExampleMethods

#em

Methods included from CoolioHelpers::ExampleHelpers

#coolio

Methods included from AMQPHelpers::ExampleMethods

#amqp

Class Method Details

.included(example_group) ⇒ Object



96
97
98
99
100
# File 'lib/evented-spec/spec_helper.rb', line 96

def self.included(example_group)
  unless example_group.respond_to? :default_timeout
    example_group.extend GroupMethods
  end
end

Instance Method Details

#default_optionsHash

Retrieves default options passed in from enclosing example groups

Returns:

  • (Hash)

    default option for currently running example



105
106
107
# File 'lib/evented-spec/spec_helper.rb', line 105

def default_options
  @default_options ||= self.class.default_options.dup rescue {}
end

#delayed(time, &block) ⇒ Object

Executes an operation after certain delay

Parameters:

  • time (Float)

    to wait before operation



112
113
114
115
116
# File 'lib/evented-spec/spec_helper.rb', line 112

def delayed(time, &block)
  @evented_example.delayed(time) do
    @example_group_instance.instance_eval(&block)
  end
end

#done(*args, &block) ⇒ Object

Breaks the event loop and finishes the spec. This should be called after you are reasonably sure that your expectations succeeded. Done yields to any given block first, then stops EM event loop. For amqp specs, stops AMQP and cleans up AMQP state.

You may pass delay (in seconds) to done. If you do so, please keep in mind that your (default or explicit) spec timeout may fire before your delayed done callback is due, leading to SpecTimeoutExceededError

Parameters:

  • Delay (Float)

    before event loop is stopped



128
129
130
# File 'lib/evented-spec/spec_helper.rb', line 128

def done(*args, &block)
  @evented_example.done *args, &block if @evented_example
end

#timeout(*args) ⇒ Object

Manually sets timeout for currently running example. If spec doesn’t call #done before timeout, it is marked as failed on timeout.

Parameters:

  • Delay (Float)

    before event loop is stopped with error



136
137
138
# File 'lib/evented-spec/spec_helper.rb', line 136

def timeout(*args)
  @evented_example.timeout *args if @evented_example
end