File: spec_helpers.rb

package info (click to toggle)
ruby-flipper 0.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,288 kB
  • sloc: ruby: 16,377; sh: 61; javascript: 24; makefile: 14
file content (85 lines) | stat: -rw-r--r-- 1,825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
require 'ice_age'
require 'json'
require 'rack/test'

module SpecHelpers
  def self.included(base)
    base.let(:flipper) { build_flipper }
    base.let(:app) { build_app(flipper) }
  end

  def build_app(flipper, options = {})
    Flipper::UI.app(flipper, options) do |builder|
      builder.use Rack::Session::Cookie, secret: 'test'
    end
  end

  def build_api(flipper, options = {})
    Flipper::Api.app(flipper, options)
  end

  def build_flipper(adapter = build_memory_adapter)
    Flipper.new(adapter)
  end

  def build_memory_adapter
    Flipper::Adapters::Memory.new
  end

  def json_response
    JSON.parse(last_response.body)
  end

  def api_error_code_reference_url
    'https://flippercloud.io/docs/api#error-code-reference'
  end

  def api_not_found_response
    {
      'code' => 1,
      'message' => 'Feature not found.',
      'more_info' => api_error_code_reference_url,
    }
  end

  def api_flipper_id_is_missing_response
    {
      'code' => 4,
      'message' => 'Required parameter flipper_id is missing.',
      'more_info' => api_error_code_reference_url,
    }
  end

  def api_positive_percentage_error_response
    {
      'code' => 3,
      'message' => 'Percentage must be a positive number less than or equal to 100.',
      'more_info' => api_error_code_reference_url,
    }
  end

  def silence
    # Store the original stderr and stdout in order to restore them later
    original_stderr = $stderr
    original_stdout = $stdout

    # Redirect stderr and stdout
    output = $stderr = $stdout = StringIO.new

    yield

    $stderr = original_stderr
    $stdout = original_stdout

    # Return output
    output.string
  end
end

RSpec.configure do |config|
  config.order = :random
  Kernel.srand config.seed

  config.include Rack::Test::Methods
  config.include SpecHelpers
end