Class: Rack::ClientSpec::TestCase

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/rack/client_spec/test_case.rb

Direct Known Subclasses

LobsterSpec

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert

Constructor Details

#initialize(got_sequence = nil) ⇒ TestCase

Returns a new instance of TestCase.



31
32
33
34
35
# File 'lib/rack/client_spec/test_case.rb', line 31

def initialize(got_sequence = nil)
  @got_sequence = got_sequence
  @run_test = !got_sequence.nil?
  @sequence = []
end

Instance Attribute Details

#sequenceObject (readonly)

Returns the value of attribute sequence.



29
30
31
# File 'lib/rack/client_spec/test_case.rb', line 29

def sequence
  @sequence
end

Class Method Details

.make_sequencesObject



11
12
13
14
15
16
17
# File 'lib/rack/client_spec/test_case.rb', line 11

def self.make_sequences()
  sequences = {}
  self.instance_methods.grep(/^test_/).map do |name|
    sequences[name] = self.new(nil).call_test_method(name, test: false).sequence
  end
  sequences
end

.run_sequence(name, sequence) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rack/client_spec/test_case.rb', line 19

def self.run_sequence(name, sequence)
  r = nil
  begin
    self.new(sequence).call_test_method(name, test: true)
  rescue Assertion => e
    r = e
  end
  Result.new r
end

Instance Method Details

#assert_request(expect_request, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rack/client_spec/test_case.rb', line 37

def assert_request(expect_request, &block)
  got = @got_sequence.shift
  unless expect_request.match? got.env
    raise ArgumentError, "Request is not matched with the expected request"
  end
  block.yield got.env, got.response
end

#call_test_method(name, test: nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/rack/client_spec/test_case.rb', line 45

def call_test_method(name, test: nil)
  @run_test = test unless test.nil?
  setup if respond_to? :setup
  send name
  self
end

#route(method, path, env, &block) ⇒ Object



52
53
54
55
56
# File 'lib/rack/client_spec/test_case.rb', line 52

def route(method, path, env, &block)
  request = ExpectRequest.new(method, path, env)
  @sequence << request
  assert_request request, &block if @run_test
end