Commit ec0b6ff7 by andrew morton

Baby steps towards passing request data through the metadata

parent 243346d3
# RSpec Swagger
The design of this is heavily influenced by the awesome [swagger_rails](https://github.com/domaindrivendev/swagger_rails) gem.
## Running tests
Set up a test site for a specific version of Rails:
```
RAILS_VERSION=4.2.0
./make_site.sh
```
Re-run the tests:
```
bundle exec rspec
```
require 'rspec/core'
require 'rspec/swagger/formatter'
require 'rspec/swagger/version' require 'rspec/swagger/version'
module RSpec module RSpec
......
require 'rspec/core/formatters/base_text_formatter'
module RSpec
module Swagger
class Formatter < RSpec::Core::Formatters::BaseTextFormatter
RSpec::Core::Formatters.register self,
:example_group_started, :example_group_finished, :example_finished
def initialize(output)
super
@document = {}
end
def watching?
!!@watching
end
def example_group_started(notification)
@watching = notification.group.metadata[:type] == :request
return unless watching?
pp notification.group.metadata
end
def example_finished(notification)
return unless watching?
# pp notification
end
def example_group_finished(notification)
@watching = true
end
end
end
end
require 'rspec/swagger' require 'spec_helper'
RSpec.describe RSpec::Swagger do RSpec.describe RSpec::Swagger do
it "loads" do it "loads" do
......
require 'rails_helper' require 'rails_helper'
def build path, params = {}, headers = {} module SwaggerPath
if Rails::VERSION::MAJOR >= 5 def path template, &block
[path, { params: params, headers: headers }] describe "path #{template}", {swagger_path: template}, &block
else
[path, params, headers]
end end
end end
RSpec.describe "Requestsing", type: :request do module SwaggerOperation
describe "GET /posts" do def operation verb, &block
it "works! (now write some real specs)" do verb = verb.to_s.downcase
get(*build('/posts', {}, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'})) describe verb.to_s, {swagger_operation: verb.to_sym}, &block
expect(response).to have_http_status(200)
end
end end
end
module SwaggerRequest
def test_request code, params = {}, headers = {}
path = metadata[:swagger_path]
method = metadata[:swagger_operation]
describe "POST /posts" do # binding.pry
it "works! (now write some real specs)" do args = if Rails::VERSION::MAJOR >= 5
post(*build('/posts', { post: { title: 'asdf', body: "blah" } }.to_json, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'})) [path, { params: params, headers: headers }]
expect(response).to have_http_status(201) else
[path, params, headers]
end
it "does stuff" do
# binding.pry
self.send(method, *args)
expect(response).to have_http_status(code)
end end
end end
end
RSpec.configure do |c|
c.extend SwaggerPath, type: :request
c.extend SwaggerOperation, :swagger_path
c.extend SwaggerRequest, :swagger_operation
end
RSpec.describe "Requestsing", type: :request do
path '/posts' do
operation "GET" do
test_request(200, {}, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'})
end
operation "POST" do
test_request(201, { post: { title: 'asdf', body: "blah" } }.to_json, {'CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'})
end
end
end end
require "spec_helper"
RSpec.describe RSpec::Swagger::Formatter do
let(:output) { StringIO.new }
let(:formatter) { described_class.new(output) }
let(:group_notification) { double(group: group) }
let(:group) { double(metadata: metadata) }
let(:metadata) { {} }
describe "#example_group_started" do
context "groups with no type" do
let(:metadata) { {} }
it "ignores" do
formatter.example_group_started(group_notification)
expect(formatter).not_to be_watching
end
end
context "groups with type request" do
let(:metadata) { {type: :request} }
it "watches" do
formatter.example_group_started(group_notification)
expect(formatter).to be_watching
end
end
end
describe "#example_group_finished" do
let(:metadata) { {} }
it "resets to watching" do
formatter.example_group_started(group_notification)
expect(formatter).not_to be_watching
formatter.example_group_finished(group_notification)
expect(formatter).to be_watching
end
end
end
require 'rspec/swagger'
# This file was generated by the `rspec --init` command. Conventionally, all # This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause # The generated `.rspec` file contains `--require spec_helper` which will cause
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment