|
5 | 5 | RSpec.describe Pragma::Rails::Controller do
|
6 | 6 | subject { controller_klass.new.tap(&:run_operation) }
|
7 | 7 |
|
8 |
| - let(:controller_klass) do |
9 |
| - Class.new do |
10 |
| - attr_reader :render_args |
11 |
| - |
12 |
| - def run_operation |
13 |
| - operation = Class.new(Pragma::Operation::Base) do |
14 |
| - step :respond! |
15 |
| - |
16 |
| - def respond!(options, params:, **) |
17 |
| - options['result.response'] = Pragma::Operation::Response::Created.new( |
18 |
| - headers: { 'X-Foo' => params[:foo] }, |
19 |
| - entity: { foo: params[:foo] } |
20 |
| - ) |
| 8 | + context 'when the operation executes successfully' do |
| 9 | + let(:controller_klass) do |
| 10 | + Class.new do |
| 11 | + attr_reader :render_args |
| 12 | + |
| 13 | + def run_operation |
| 14 | + operation = Class.new(Pragma::Operation::Base) do |
| 15 | + step :respond! |
| 16 | + |
| 17 | + def respond!(options, params:, **) |
| 18 | + options['result.response'] = Pragma::Operation::Response::Created.new( |
| 19 | + headers: { 'X-Foo' => params[:foo] }, |
| 20 | + entity: { foo: params[:foo] } |
| 21 | + ) |
| 22 | + end |
21 | 23 | end
|
| 24 | + |
| 25 | + run operation |
22 | 26 | end
|
23 | 27 |
|
24 |
| - run operation |
25 |
| - end |
| 28 | + def response |
| 29 | + @response ||= OpenStruct.new(headers: {}) |
| 30 | + end |
26 | 31 |
|
27 |
| - def response |
28 |
| - @response ||= OpenStruct.new(headers: {}) |
29 |
| - end |
| 32 | + def render(*args) |
| 33 | + @render_args = args |
| 34 | + end |
30 | 35 |
|
31 |
| - def render(*args) |
32 |
| - @render_args = args |
33 |
| - end |
| 36 | + def params |
| 37 | + {} |
| 38 | + end |
| 39 | + |
| 40 | + protected |
34 | 41 |
|
35 |
| - def params |
36 |
| - {} |
| 42 | + def operation_params |
| 43 | + ActionController::Parameters.new(foo: 'bar') |
| 44 | + end |
| 45 | + end.tap do |klass| |
| 46 | + klass.include described_class |
37 | 47 | end
|
| 48 | + end |
38 | 49 |
|
39 |
| - protected |
| 50 | + it 'responds with the headers from the operation' do |
| 51 | + expect(subject.response.headers).to eq('X-Foo' => 'bar') |
| 52 | + end |
40 | 53 |
|
41 |
| - def operation_params |
42 |
| - ActionController::Parameters.new(foo: 'bar') |
43 |
| - end |
44 |
| - end.tap do |klass| |
45 |
| - klass.include described_class |
| 54 | + it 'responds with the status code from the operation' do |
| 55 | + expect(subject.render_args.first[:status]).to eq(201) |
46 | 56 | end
|
47 |
| - end |
48 | 57 |
|
49 |
| - it 'responds with the headers from the operation' do |
50 |
| - expect(subject.response.headers).to eq('X-Foo' => 'bar') |
| 58 | + it 'responds with the resource from the operation' do |
| 59 | + expect(subject.render_args.first[:json]).to eq('foo' => 'bar') |
| 60 | + end |
51 | 61 | end
|
52 | 62 |
|
53 |
| - it 'responds with the status code from the operation' do |
54 |
| - expect(subject.render_args.first[:status]).to eq(201) |
55 |
| - end |
| 63 | + context 'when the operation does not set a result.response skill' do |
| 64 | + let(:controller_klass) do |
| 65 | + Class.new do |
| 66 | + attr_reader :render_args |
| 67 | + |
| 68 | + def run_operation |
| 69 | + operation = Class.new(Pragma::Operation::Base) |
| 70 | + run operation |
| 71 | + end |
56 | 72 |
|
57 |
| - it 'responds with the resource from the operation' do |
58 |
| - expect(subject.render_args.first[:json]).to eq('foo' => 'bar') |
| 73 | + protected |
| 74 | + |
| 75 | + def operation_params |
| 76 | + ActionController::Parameters.new(foo: 'bar') |
| 77 | + end |
| 78 | + end.tap do |klass| |
| 79 | + klass.include described_class |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + it 'raises a NoResponseError' do |
| 84 | + expect { subject }.to raise_error(Pragma::Rails::NoResponseError) |
| 85 | + end |
59 | 86 | end
|
60 | 87 | end
|
0 commit comments