Skip to content

Support Rails Engines? #410

@orionstudt

Description

@orionstudt

I seem to have encountered an issue that makes it appear like Rails Engines are not supported here?

When you mount an engine it creates a method that points at an action dispatch routing proxy based on the name of your engine. But as soon as I register an observer for OpenapiFirst on my app within ActionDispatch::IntegrationTest, this method no longer exists.

As an example, my config/routes.rb file might be like:

# config/routes.rb
Rails.application.routes.draw do
  mount API::Engine, at: "/api", as: "api_engine"
end

And then in my test I can call the engine when I invoke rack-test like:

# engines/api/test/controllers/estimates_controller_test.rb
class API::EstimatesControllerTest < ApiIntegrationTest
  test "sample test" do
    get(api_engine.v1_estimates_path, params: ...)
  end
end

Now if I don't setup OpenapiFirst at all and set a breakpoint inside the test, I can check the values of app and api_engine:

[1] pry(#<API::EstimatesControllerTest>)> app
=> #<Wonder::Application>
[2] pry(#<API::EstimatesControllerTest>)> api_engine
=> #<ActionDispatch::Routing::RoutesProxy:0x000000017fb6d750

However if I setup OpenapiFirst in my test/test_helper.rb file like so:

# test/test_helper.rb
require_relative "../config/environment"
require "rails/test_help"

require "openapi_first"
OpenapiFirst::Test.setup do |test|
  test.register Rails.root.join("doc/api/openapi.yml").to_s
  test.report_coverage = false
end

module ActionDispatch
  class IntegrationTest
    include OpenapiFirst::Test::Methods[Wonder::Application]
  end
end

class ApiIntegrationTest < ActionDispatch::IntegrationTest
  # more setup ...
end

Then api_engine is no longer defined inside my test, and my tests fail with NameError.

[1] pry(#<API::EstimatesControllerTest>)> app
=> #<OpenapiFirst::Middlewares::ResponseValidation:0x0000000303a2d540
 @app=
  #<OpenapiFirst::Middlewares::RequestValidation:0x000000017e6948d8
   @app=Wonder::Application,
[2] pry(#<API::EstimatesControllerTest>)> api_engine
NameError: undefined local variable or method 'api_engine' for an instance of API::EstimatesControllerTest

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions