diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 54d384483..46d3b0964 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 5 ## main +* Add support for `Devise::Test::ControllerHelpers` by resolving `NoMethodError: undefined method env for nil:NilClass` + + *fugufish* + * Refer to `helpers` in `NameError` message in development and test environments. *Simon Fish* diff --git a/lib/view_component/test_helpers.rb b/lib/view_component/test_helpers.rb index 1cb733451..e450b5cbc 100644 --- a/lib/view_component/test_helpers.rb +++ b/lib/view_component/test_helpers.rb @@ -75,7 +75,7 @@ def render_inline(component, **args, &block) # @param params [Hash] Parameters to be passed to the preview. # @return [Nokogiri::HTML] def render_preview(name, from: __vc_test_helpers_preview_class, params: {}) - previews_controller = __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize) + previews_controller = __vc_render_preview_controller # From what I can tell, it's not possible to overwrite all request parameters # at once, so we set them individually here. @@ -225,6 +225,16 @@ def vc_test_request end end + def before_setup + @request = __vc_render_preview_controller.request + super + end + + def teardown + super + @vc_render_preview_controller = nil + end + # Note: We prefix private methods here to prevent collisions in consumer's tests. private @@ -244,5 +254,9 @@ def __vc_test_helpers_preview_class rescue NameError raise NameError, "`render_preview` expected to find #{result}, but it does not exist." end + + def __vc_render_preview_controller + @vc_render_preview_controller ||= __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize) + end end end