From cdc1717b5cc256307084d77c0cf81c8c4d749bad Mon Sep 17 00:00:00 2001 From: Jack Rothrock Date: Wed, 24 Jan 2024 12:14:37 -0700 Subject: [PATCH] Add tests --- scout_apm.gemspec | 2 + test/test_helper.rb | 6 ++ test/unit/instruments/action_view_test.rb | 90 +++++++++++++++++++ .../instruments/fixtures/simple_view.html.erb | 9 ++ .../fixtures/test/_simple_partial.html.erb | 3 + 5 files changed, 110 insertions(+) create mode 100644 test/unit/instruments/action_view_test.rb create mode 100644 test/unit/instruments/fixtures/simple_view.html.erb create mode 100644 test/unit/instruments/fixtures/test/_simple_partial.html.erb diff --git a/scout_apm.gemspec b/scout_apm.gemspec index b33a6baa..57e7c2f5 100644 --- a/scout_apm.gemspec +++ b/scout_apm.gemspec @@ -34,6 +34,8 @@ Gem::Specification.new do |s| # tests. Specific versions are pulled in using specific gemfiles, e.g. # `gems/rails3.gemfile`. s.add_development_dependency "activerecord" + s.add_development_dependency "actionpack" + s.add_development_dependency "actionview" s.add_development_dependency "sqlite3" s.add_development_dependency "rubocop" diff --git a/test/test_helper.rb b/test/test_helper.rb index dca6abc3..49e69370 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -65,7 +65,13 @@ def method_missing(sym) end end +def remove_rails_namespace + Object.send(:remove_const, "Rails") if defined?(Rails) +end + def fake_rails(version) + remove_rails_namespace + Kernel.const_set("Rails", Module.new) Kernel.const_set("ActionController", Module.new) r = Kernel.const_get("Rails") diff --git a/test/unit/instruments/action_view_test.rb b/test/unit/instruments/action_view_test.rb new file mode 100644 index 00000000..b44cc93b --- /dev/null +++ b/test/unit/instruments/action_view_test.rb @@ -0,0 +1,90 @@ +require 'test_helper' +require 'action_view' +require 'action_pack' +require 'action_controller' + +FIXTURE_LOAD_PATH = File.expand_path("fixtures", __dir__) + +include ActionView::Context +include ActionView::Helpers::TagHelper +include ActionView::Helpers::TextHelper + +module ActionController + + class Base + self.view_paths = FIXTURE_LOAD_PATH + + def self.test_routes(&block) + routes = ActionDispatch::Routing::RouteSet.new + routes.draw(&block) + include routes.url_helpers + routes + end + end + + class TestCase + include ActionDispatch::TestProcess + + def self.with_routes(&block) + routes = ActionDispatch::Routing::RouteSet.new + routes.draw(&block) + include Module.new { + define_method(:setup) do + super() + @routes = routes + @controller.singleton_class.include @routes.url_helpers if @controller + end + } + routes + end + end +end + +class TestController < ActionController::Base + + def render_simple_view + render template: "simple_view" + end +end + +class RenderTest < ActionController::TestCase + + tests TestController + + with_routes do + get :render_simple_view, to: "test#render_simple_view" + end + + def setup + # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get + # a more accurate simulation of what happens in "real life". + super + @controller.logger = ActiveSupport::Logger.new(nil) + ActionView::Base.logger = ActiveSupport::Logger.new(nil) + + @request.host = "www.scoutapm.com" + + @old_view_paths = ActionController::Base.view_paths + ActionController::Base.view_paths = FIXTURE_LOAD_PATH + end + + def teardown + ActionView::Base.logger = nil + + ActionController::Base.view_paths = @old_view_paths + end + + def test_partial_instrumentation + recorder = FakeRecorder.new + agent_context.recorder = recorder + + instrument = ScoutApm::Instruments::ActionView.new(agent_context) + instrument.install(prepend: true) + + get :render_simple_view + assert_response :success + + assert_equal "simple_view/Rendering", recorder.requests.first.root_layer.name + assert_equal "test/_simple_partial/Rendering", recorder.requests.first.root_layer.children.first.name + end +end diff --git a/test/unit/instruments/fixtures/simple_view.html.erb b/test/unit/instruments/fixtures/simple_view.html.erb new file mode 100644 index 00000000..0baa1f2a --- /dev/null +++ b/test/unit/instruments/fixtures/simple_view.html.erb @@ -0,0 +1,9 @@ + + + Simple View + + +

Simple View

+ <%= render partial: "simple_partial", locals: { message: 'Simple Partial' } %> + + \ No newline at end of file diff --git a/test/unit/instruments/fixtures/test/_simple_partial.html.erb b/test/unit/instruments/fixtures/test/_simple_partial.html.erb new file mode 100644 index 00000000..8c6b0abf --- /dev/null +++ b/test/unit/instruments/fixtures/test/_simple_partial.html.erb @@ -0,0 +1,3 @@ +
+

<%= @message %>

+