Share the view context in tests #2410
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What are you trying to accomplish?
Fixes: #2408
This PR addresses an issue causing out-of-order rendering issues in tests when the test attempts to re-use the view context provided by the test controller.
The test controller does not memoize the view context, so calling
vc_test_controller.view_context
always returns a new instance. If this method is called by the test code, any subsequent calls to eg.render_inline
will create a new view context and append to its buffer while the test code appends to a different buffer.This was an issue in v3 as well, but could be avoided by enabling the capture compatibility patch. Since v4 has removed the patch, there is no way to avoid the problem (although very few people will get bitten by this).
What approach did you choose and why?
I propose introducing a new method,
vc_test_view_context
, that can be called both by test code andrender_inline
so that both can reference the same shared view context. This shared context is reset after each call torender_inline
, meaning test code will have to call it again if additional calls torender_inline
are made. This is not ideal, but all the other avenues I explored were less ergonomic or required even more knowledge of the inner workings of ActionView and ViewComponent.Todo
I suspect thewith_variant
andwith_format
helpers are not actually working as expected since they set values onvc_test_controller.view_context
, which returns a view context that is immediately discarded 😬I need to look at this further.The
with_variant
andwith_format
helpers actually set values on the lookup context, which, confusingly, is shared between view context instances. That's why they work as expected.