thoughtbot / shoulda-context

Shoulda Context makes it easy to write understandable and maintainable tests under Minitest and Test::Unit within Rails projects or plain Ruby projects.
MIT License
239 stars 58 forks source link

Remove 'assigned but unused variable' warning #61

Closed mcmire closed 4 years ago

mcmire commented 4 years ago

In converting a should within a context into a Minitest test (i.e. method), we have to make use of class_eval. Inside of this eval we refer to the Context object that we're inside of, which means we have to set a variable outside of the eval. Unfortunately Ruby can't see into the eval so it thinks that variable has gone unused and emits a warning. The only real way to fix this is to make use of the variable somehow. Double assigning the variable seems to do the trick.

Hat tip: @brandur, @pyrmont

brandur commented 4 years ago

Thanks for the quick work @mcmire! I was a little afraid when I originally commented that this project might be undermaintained, but I see that's far from the case.

Just in case you didn't see it: @pyrmont suggested an alternative solution that involves a double-assignment trick in https://github.com/thoughtbot/shoulda-context/issues/57#issuecomment-574918584. It has the same effect, and is maybe a little less gnarly. Totally up to you on which way to go though.

mcmire commented 4 years ago

Thanks @brandur! I saw it just now and integrated it.

brandur commented 4 years ago

Nice! LGTM.