Class: Aspec::Test

Inherits:
Object
  • Object
show all
Includes:
RSpec::Mocks::ExampleMethods, Rack::Test::Methods
Defined in:
lib/aspec/test.rb

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ Test

Returns a new instance of Test


7
8
9
# File 'lib/aspec/test.rb', line 7

def initialize(steps)
  @steps = steps
end

Instance Method Details

#appObject


23
24
25
# File 'lib/aspec/test.rb', line 23

def app
  @app
end

#comment_only?Boolean

Returns:

  • (Boolean)

15
16
17
# File 'lib/aspec/test.rb', line 15

def comment_only?
  @steps.all? {|s| s[:comment]}
end

#contains_line?(line_num) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/aspec/test.rb', line 19

def contains_line?(line_num)
  @steps.first[:line_num] <= line_num and @steps.last[:line_num] >= line_num
end

#run(config) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/aspec/test.rb', line 27

def run(config)
  formatter  = config.formatter
  is_slow    = config.slow?
  @app       = config.get_app_under_test.call
  start_time = Time.at(0)
  failed = false
  @steps.each_with_index do |step, time_delta|
    if is_slow
      sleep 0.5
    end

    if ARGV.include?("--debug")
      formatter.debug(step)
    end

    if failed
      formatter.step_error_title(step)
      next
    end

    if step[:comment]
      formatter.comment(step[:comment])
    else
      allow(Time).to receive(:now).and_return(start_time + 2*time_delta)

      begin
        if step[:method][0] == ">"
          method = step[:method][1..-1]
          validate_method(method)
          url = "http://" + step[:url]
          FakeWeb.register_uri(method.downcase.to_sym, url,
              :body => step[:exp_response],
              :content_type => step[:exp_content_type]
            )
        else
          validate_method(step[:method])
          send(step[:method].downcase, step[:url])
        end
      rescue Object => e
        formatter.exception("  " + e.class.to_s + ": " + e.message)
        e.backtrace.each do |backtrace_line|
          formatter.exception("  " + backtrace_line) unless backtrace_line =~ /vendor\/bundle/ or backtrace_line =~ /test.rb/
        end
        failed = true
      end

      unless failed or step[:method][0] == ">"
        if last_response.status.to_s != step[:exp_status]
          formatter.exception(" * Expected status #{step[:exp_status]} got #{last_response.status}")
          failed = true
        end

        if step[:exp_content_type] == "application/json" && !step[:resp_is_regex]
          begin
            expected_object = JSON.parse(step[:exp_response])
            begin
              response_object = JSON.parse(last_response.body)
              if expected_object != response_object
                formatter.exception(" * Expected response #{JSON.pretty_generate(expected_object)} got #{JSON.pretty_generate(response_object)}")
                formatter.exception(" * Diff #{JSON.pretty_generate(JsonCompare.get_diff(expected_object, response_object))}")
                failed = true
              end
            rescue JSON::ParserError
              formatter.exception(" * Response did not parse correctly as JSON: #{last_response.body.inspect}")
              failed = true
            end
          rescue JSON::ParserError
            formatter.exception(" * Expectation did not parse correctly as JSON: #{step[:exp_response].inspect}")
            failed = true
          end

        else

          if step[:resp_is_regex]
            pattern = nil, body = nil
            if !(step[:exp_content_type].start_with? 'text/')
              pattern = Regexp.new(step[:exp_response].force_encoding("ASCII-8BIT"), Regexp::FIXEDENCODING)
              body = last_response.body.to_s.force_encoding("ASCII-8BIT")
            else
              pattern = Regexp.new(step[:exp_response])
              body = last_response.body.to_s
            end
            if !(body =~ pattern)
              formatter.exception(" * Expected response pattern #{step[:exp_response].inspect} didn't match #{last_response.body.inspect}")
              failed = true
            end
          elsif !step[:resp_is_regex] & (last_response.body.to_s != step[:exp_response])
            formatter.exception(" * Expected response #{step[:exp_response].inspect} got #{last_response.body.inspect[0..50] + "..."}")
            failed = true
          end
        end

        if step[:exp_content_type]
          exp_content_type_header = "#{step[:exp_content_type]}"
          exp_content_type_header << ";charset=utf-8" unless exp_content_type_header.start_with? "image/"
          if last_response.headers["Content-Type"] != exp_content_type_header
            formatter.exception(" * Expected content type #{exp_content_type_header} got #{last_response.headers["Content-Type"]}")
            failed = true
          end
        end
      end

      if failed
        formatter.step_error(step)
      else
        formatter.step_pass(step)
      end
    end
  end
  !failed
end

#validate_method(method) ⇒ Object


11
12
13
# File 'lib/aspec/test.rb', line 11

def validate_method(method)
  raise "unknown method #{method}" unless %W(GET POST DELETE PUT).include?(method)
end