Document the fix for view context leaking in specs. (#847)

## Description
This pull request adds documentation for fixing the view context leakage between specs. I wanted to start with a documentation fix and go from there. If we later determine we need to clear the view context for all spec types by default, we can remove this documentation. See the referenced issue for the complete history of the issue.

## Testing
N/A

## To-Dos
None

## References
* [Should automatically clear view_context before/after each spec](https://github.com/drapergem/draper/issues/814)
This commit is contained in:
Cliff Braton 2019-03-14 10:02:45 -05:00 committed by GitHub
parent 157eb95507
commit d4d6de849b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -474,6 +474,20 @@ preferred stubbing technique (this example uses RSpec's `stub` method):
helpers.stub(users_path: '/users')
```
### View context leakage
As mentioned before, Draper needs to build a view context to access helper methods. In MiniTest, the view context is
cleared during `before_setup` preventing any view context leakage. In RSpec, the view context is cleared before each
`decorator`, `controller`, and `mailer` spec. However, if you use decorators in other types of specs
(e.g. `job`), you may still experience the view context leaking from the previous spec. To solve this, add the
following to your `spec_helper` for each type of spec you are experiencing the leakage:
```ruby
config.before(:each, type: :type) { Draper::ViewContext.clear! }
```
_Note_: The `:type` above is just a placeholder. Replace `:type` with the type of spec you are experiencing
the leakage from.
## Advanced usage
### Shared Decorator Methods