Module: Trice::SpecHelper

Defined in:
lib/trice/spec_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_time(runtime, static_time, block) ⇒ Object



7
8
9
# File 'lib/trice/spec_helper.rb', line 7

def self.extract_time(runtime, static_time, block)
  block ? runtime.instance_eval(&block) : static_time
end

Instance Method Details

#set_now_as_reference_timeObject



19
20
21
# File 'lib/trice/spec_helper.rb', line 19

def set_now_as_reference_time
  set_reference_time(Time.now)
end

#set_reference_time(static_time = nil, &block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/trice/spec_helper.rb', line 11

def set_reference_time(static_time = nil, &block)
  around(:each) do |ex|
    time = SpecHelper.extract_time(self, static_time, block)

    Trice.with_reference_time(time) { ex.run }
  end
end

#stub_requested_at(static_time = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/trice/spec_helper.rb', line 23

def stub_requested_at(static_time = nil, &block)
  before(:each) do |ex|
    time = SpecHelper.extract_time(self, static_time, block)

    case ex.[:type]
    when :controller
      request.headers['X-Requested-At'] = time.iso8601
    when :feature
      case
      when page.driver.respond_to?(:header)
        # rack-test, capybara-webkit
        page.driver.header('X-Requested-At', time.iso8601)
      when page.driver.respond_to?(:add_header)
        # poltergeist
        page.driver.add_header('X-Requested-At', time.iso8601)
      else
        raise Trice::TestStubbingNotSupported, "Test stubbing for driver #{page.driver.class} is not supported"
      end
    else
      raise Trice::TestStubbingNotSupported, "Test stubbing for type: #{ex.metadata[:type]} is not supported"
    end
  end
end