|
| 1 | +Feature: Generate Textile documentation from test examples |
| 2 | + |
| 3 | + Background: |
| 4 | + Given a file named "app.rb" with: |
| 5 | + """ |
| 6 | + require 'sinatra' |
| 7 | +
|
| 8 | + class App < Sinatra::Base |
| 9 | + get '/orders' do |
| 10 | + content_type :json |
| 11 | +
|
| 12 | + [200, [{ name: 'Order 1', amount: 9.99, description: nil }, |
| 13 | + { name: 'Order 2', amount: 100.0, description: 'A great order' }].to_json] |
| 14 | + end |
| 15 | +
|
| 16 | + get '/orders/:id' do |
| 17 | + content_type :json |
| 18 | +
|
| 19 | + [200, { order: { name: 'Order 1', amount: 100.0, description: 'A great order' } }.to_json] |
| 20 | + end |
| 21 | +
|
| 22 | + post '/orders' do |
| 23 | + 201 |
| 24 | + end |
| 25 | +
|
| 26 | + put '/orders/:id' do |
| 27 | + 200 |
| 28 | + end |
| 29 | +
|
| 30 | + delete '/orders/:id' do |
| 31 | + 200 |
| 32 | + end |
| 33 | +
|
| 34 | + get '/help' do |
| 35 | + [200, 'Welcome Henry !'] |
| 36 | + end |
| 37 | + end |
| 38 | + """ |
| 39 | + And a file named "app_spec.rb" with: |
| 40 | + """ |
| 41 | + require "rspec_api_documentation" |
| 42 | + require "rspec_api_documentation/dsl" |
| 43 | +
|
| 44 | + RspecApiDocumentation.configure do |config| |
| 45 | + config.app = App |
| 46 | + config.api_name = "Example API" |
| 47 | + config.format = :textile |
| 48 | + end |
| 49 | +
|
| 50 | + resource 'Orders' do |
| 51 | + get '/orders' do |
| 52 | +
|
| 53 | + example_request 'Getting a list of orders' do |
| 54 | + status.should eq(200) |
| 55 | + response_body.should eq('[{"name":"Order 1","amount":9.99,"description":null},{"name":"Order 2","amount":100.0,"description":"A great order"}]') |
| 56 | + end |
| 57 | + end |
| 58 | +
|
| 59 | + get '/orders/:id' do |
| 60 | + let(:id) { 1 } |
| 61 | +
|
| 62 | + example_request 'Getting a specific order' do |
| 63 | + status.should eq(200) |
| 64 | + response_body.should == '{"order":{"name":"Order 1","amount":100.0,"description":"A great order"}}' |
| 65 | + end |
| 66 | + end |
| 67 | +
|
| 68 | + post '/orders' do |
| 69 | + parameter :name, 'Name of order', :required => true |
| 70 | + parameter :amount, 'Amount paid', :required => true |
| 71 | + parameter :description, 'Some comments on the order' |
| 72 | +
|
| 73 | + let(:name) { "Order 3" } |
| 74 | + let(:amount) { 33.0 } |
| 75 | +
|
| 76 | + example_request 'Creating an order' do |
| 77 | + status.should == 201 |
| 78 | + end |
| 79 | + end |
| 80 | +
|
| 81 | + put '/orders/:id' do |
| 82 | + parameter :name, 'Name of order', :required => true |
| 83 | + parameter :amount, 'Amount paid', :required => true |
| 84 | + parameter :description, 'Some comments on the order' |
| 85 | +
|
| 86 | + let(:id) { 2 } |
| 87 | + let(:name) { "Updated name" } |
| 88 | +
|
| 89 | + example_request 'Updating an order' do |
| 90 | + status.should == 200 |
| 91 | + end |
| 92 | + end |
| 93 | +
|
| 94 | + delete "/orders/:id" do |
| 95 | + let(:id) { 1 } |
| 96 | +
|
| 97 | + example_request "Deleting an order" do |
| 98 | + status.should == 200 |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +
|
| 103 | + resource 'Help' do |
| 104 | + get '/help' do |
| 105 | + example_request 'Getting welcome message' do |
| 106 | + status.should eq(200) |
| 107 | + response_body.should == 'Welcome Henry !' |
| 108 | + end |
| 109 | + end |
| 110 | +
|
| 111 | + end |
| 112 | + """ |
| 113 | + When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter` |
| 114 | + |
| 115 | + Scenario: Output helpful progress to the console |
| 116 | + Then the output should contain: |
| 117 | + """ |
| 118 | + Generating API Docs |
| 119 | + Orders |
| 120 | + GET /orders |
| 121 | + * Getting a list of orders |
| 122 | + GET /orders/:id |
| 123 | + * Getting a specific order |
| 124 | + POST /orders |
| 125 | + * Creating an order |
| 126 | + PUT /orders/:id |
| 127 | + * Updating an order |
| 128 | + DELETE /orders/:id |
| 129 | + * Deleting an order |
| 130 | + Help |
| 131 | + GET /help |
| 132 | + * Getting welcome message |
| 133 | + """ |
| 134 | + And the output should contain "6 examples, 0 failures" |
| 135 | + And the exit status should be 0 |
| 136 | + |
| 137 | + Scenario: Index file should look like we expect |
| 138 | + Then the file "doc/api/index.textile" should contain exactly: |
| 139 | + """ |
| 140 | + h1. Example API |
| 141 | +
|
| 142 | + h2. Help |
| 143 | +
|
| 144 | + * "Getting welcome message":help/getting_welcome_message.textile |
| 145 | +
|
| 146 | + h2. Orders |
| 147 | +
|
| 148 | + * "Creating an order":orders/creating_an_order.textile |
| 149 | + * "Deleting an order":orders/deleting_an_order.textile |
| 150 | + * "Getting a list of orders":orders/getting_a_list_of_orders.textile |
| 151 | + * "Getting a specific order":orders/getting_a_specific_order.textile |
| 152 | + * "Updating an order":orders/updating_an_order.textile |
| 153 | +
|
| 154 | +
|
| 155 | + """ |
| 156 | + |
| 157 | + Scenario: Example 'Creating an order' file should look like we expect |
| 158 | + Then the file "doc/api/orders/creating_an_order.textile" should contain exactly: |
| 159 | + """ |
| 160 | + h1. Orders API |
| 161 | +
|
| 162 | + h2. Creating an order |
| 163 | +
|
| 164 | + h3. POST /orders |
| 165 | +
|
| 166 | +
|
| 167 | + h3. Parameters |
| 168 | +
|
| 169 | + Name : name *- required -* |
| 170 | + Description : Name of order |
| 171 | +
|
| 172 | + Name : amount *- required -* |
| 173 | + Description : Amount paid |
| 174 | +
|
| 175 | + Name : description |
| 176 | + Description : Some comments on the order |
| 177 | +
|
| 178 | + h3. Request |
| 179 | +
|
| 180 | + h4. Headers |
| 181 | +
|
| 182 | + <pre>Host: example.org |
| 183 | + Content-Type: application/x-www-form-urlencoded |
| 184 | + Cookie: </pre> |
| 185 | +
|
| 186 | + h4. Route |
| 187 | +
|
| 188 | + <pre>POST /orders</pre> |
| 189 | +
|
| 190 | +
|
| 191 | + h4. Body |
| 192 | +
|
| 193 | + <pre>name=Order+3&amount=33.0</pre> |
| 194 | +
|
| 195 | +
|
| 196 | + h3. Response |
| 197 | +
|
| 198 | + h4. Headers |
| 199 | +
|
| 200 | + <pre>X-Frame-Options: sameorigin |
| 201 | + X-XSS-Protection: 1; mode=block |
| 202 | + Content-Type: text/html;charset=utf-8 |
| 203 | + Content-Length: 0</pre> |
| 204 | +
|
| 205 | + h4. Status |
| 206 | +
|
| 207 | + <pre>201 Created</pre> |
| 208 | +
|
| 209 | +
|
| 210 | +
|
| 211 | +
|
| 212 | + """ |
| 213 | + |
| 214 | + Scenario: Example 'Deleting an order' file should be created |
| 215 | + Then a file named "doc/api/orders/deleting_an_order.textile" should exist |
| 216 | + |
| 217 | + Scenario: Example 'Getting a list of orders' file should be created |
| 218 | + Then a file named "doc/api/orders/getting_a_list_of_orders.textile" should exist |
| 219 | + |
| 220 | + Scenario: Example 'Getting a specific order' file should be created |
| 221 | + Then a file named "doc/api/orders/getting_a_specific_order.textile" should exist |
| 222 | + |
| 223 | + Scenario: Example 'Updating an order' file should be created |
| 224 | + Then a file named "doc/api/orders/updating_an_order.textile" should exist |
| 225 | + |
| 226 | + Scenario: Example 'Getting welcome message' file should be created |
| 227 | + Then a file named "doc/api/help/getting_welcome_message.textile" should exist |
| 228 | + |
| 229 | + |
0 commit comments