|
3 | 3 | require 'minitest'
|
4 | 4 |
|
5 | 5 | RSpec.describe OpenapiFirst::Test do
|
| 6 | + let(:definition) { OpenapiFirst.load('./examples/openapi.yaml') } |
| 7 | + |
| 8 | + let(:app) do |
| 9 | + Class.new do |
| 10 | + def call(_env) |
| 11 | + Rack::Response.new.finish |
| 12 | + end |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + describe 'Callable[]' do |
| 17 | + it 'returns a Module that can call the api' do |
| 18 | + described_class.register(definition, as: :some) |
| 19 | + mod = described_class::Callable[api: :some] |
| 20 | + app.prepend(mod) |
| 21 | + |
| 22 | + expect(definition).to receive(:validate_request) |
| 23 | + expect(definition).to receive(:validate_response) |
| 24 | + |
| 25 | + app.new.call({}) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + describe '.observe' do |
| 30 | + it 'injects request/response validation in the app' do |
| 31 | + described_class.register(definition, as: :some) |
| 32 | + described_class.observe(app, api: :some) |
| 33 | + |
| 34 | + expect(definition).to receive(:validate_request) |
| 35 | + expect(definition).to receive(:validate_response) |
| 36 | + |
| 37 | + app.new.call({}) |
| 38 | + end |
| 39 | + |
| 40 | + it 'injects request/response validation just once' do |
| 41 | + described_class.register(definition, as: :some) |
| 42 | + 2.times { described_class.observe(app, api: :some) } |
| 43 | + |
| 44 | + expect(definition).to receive(:validate_request).once |
| 45 | + expect(definition).to receive(:validate_response).once |
| 46 | + |
| 47 | + app.new.call({}) |
| 48 | + end |
| 49 | + end |
| 50 | + |
6 | 51 | describe '.minitest?' do
|
7 | 52 | it 'detects minitest' do
|
8 | 53 | test_case = Class.new(Minitest::Test)
|
|
81 | 126 | expect(described_class.definitions[:default].filepath).to eq(OpenapiFirst.load('./examples/openapi.yaml').filepath)
|
82 | 127 | end
|
83 | 128 |
|
| 129 | + it 'observes an app' do |
| 130 | + described_class.setup do |test| |
| 131 | + test.register(definition, as: :some) |
| 132 | + test.observe(app, api: :some) |
| 133 | + end |
| 134 | + |
| 135 | + expect(definition).to receive(:validate_request) |
| 136 | + app.new.call({}) |
| 137 | + end |
| 138 | + |
84 | 139 | it 'sets up minimum_coverage' do
|
85 | 140 | described_class.setup do |test|
|
86 | 141 | test.register('./examples/openapi.yaml')
|
|
0 commit comments