Skip to content

Commit

Permalink
Fix methods shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Dec 22, 2023
1 parent 4f7fdf4 commit 979dc43
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ jobs:
run: bundle exec rspec
- name: Run RSpec examples
run: CI=1 ruby examples/rspec.rb
- name: Fix RubyGems activating older versions of gems
run: gem install timeout net-protocol stringio psych date
- name: Run RSpec Rails examples
run: CI=1 ruby examples/rspec_rails.rb
- name: Run minitest example
run: CI=1 ruby examples/minitest.rb
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning].

## [Unreleased]

### Fixed

- Better checks to automatic request/response detection to prevent methods overrides via RSpec helpers (i.e. `subject(:response)`). ([@skryukov])

## [0.2.1] - 2023-10-23

### Fixed
Expand Down
4 changes: 1 addition & 3 deletions examples/minitest.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) if ENV["CI"] == "1"

require "bundler/inline"

gemfile do
source "https://rubygems.org"
gem "minitest"
gem "rack-test"
gem "skooma"
gem "skooma", (ENV["CI"] == "1") ? {path: File.join(__dir__, "..")} : {}
gem "sinatra"
end

Expand Down
5 changes: 1 addition & 4 deletions examples/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) if ENV["CI"] == "1"

require "bundler/inline"

gemfile do
source "https://rubygems.org"
gem "rspec"
gem "rack-test"
gem "skooma"
gem "skooma", (ENV["CI"] == "1") ? {path: File.join(__dir__, "..")} : {}
gem "sinatra"
end

Expand All @@ -21,7 +19,6 @@
path_to_openapi = File.join(__dir__, "openapi.yml")
config.include Skooma::RSpec[path_to_openapi], type: :request

# `rspec-rails` is also supported, so you can use it instead of `rack-test`
config.include Rack::Test::Methods, type: :request
end

Expand Down
74 changes: 74 additions & 0 deletions examples/rspec_rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

require "bundler/inline"

gemfile do
source "https://rubygems.org"
gem "rails"
gem "rspec"
gem "rspec-rails"
gem "skooma", (ENV["CI"] == "1") ? {path: File.join(__dir__, "..")} : {}
gem "sinatra"
end

require_relative "test_app"

require "rails"
require "action_controller/railtie"
require "rails/test_unit/railtie"

require "rspec/rails"
require "rspec/autorun"
require "skooma"

ENV["RAILS_ENV"] = "test"

RSpec.configure do |config|
path_to_openapi = File.join(__dir__, "openapi.yml")
config.include Skooma::RSpec[path_to_openapi], type: :request
end

class RailsApp < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
config.eager_load = false
config.logger = Logger.new(nil)

routes.append do
mount TestApp, at: "/"
end
end

RailsApp.initialize!

describe "Rails app", type: :request do
describe "OpenAPI document", type: :request do
subject(:schema) { skooma_openapi_schema }

it { is_expected.to be_valid_document }
end

describe "GET /" do
subject { get "/" }

it { is_expected.to conform_schema(200) }

it "returns correct response" do
subject
expect(response.parsed_body).to eq({"foo" => "bar"})
end
end

describe "POST /" do
subject { post("/", params: body, as: :json) }

let(:body) { {foo: "bar"} }

it { is_expected.to conform_schema(201) }

context "with invalid params" do
let(:body) { {foo: "baz"} }

it { is_expected.to conform_response_schema(400) }
end
end
end
2 changes: 1 addition & 1 deletion lib/skooma/matchers/conform_response_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def matches?(*)
end

def failure_message
return "Expected #{@expected} status code" unless status_matches?
return "Expected #{@expected} status code, but got #{@mapped_response["response"]["status"]}" unless status_matches?

super
end
Expand Down
8 changes: 4 additions & 4 deletions lib/skooma/matchers/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ def mapped_response(with_response: true, with_request: true)

def request_object
# `rails` integration
return request if defined?(::ActionDispatch)
return @request if defined?(::ActionDispatch) && @request.is_a?(::ActionDispatch::Request)
# `rack-test` integration
return last_request if defined?(::Rack::Test)
return last_request if defined?(::Rack::Test) && defined?(:last_request)

raise "Request object not found"
end

def response_object
# `rails` integration
return response if defined?(::ActionDispatch)
return @response if defined?(::ActionDispatch) && @response.is_a?(::ActionDispatch::Response)
# `rack-test` integration
return last_response if defined?(::Rack::Test)
return last_response if defined?(::Rack::Test) && defined?(:last_response)

raise "Response object not found"
end
Expand Down

0 comments on commit 979dc43

Please sign in to comment.