-
-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Differentiating between test constants and context variables #127
Comments
Correct.
You can say that is "reset", even though none of this is exactly correct. class C
a = 'hi'
define_method(:p) { puts a }
define_method(:r) { a.reverse! }
end
x = C.new
y = C.new
x.p # => hi
y.p # => hi
x.r
y.p # => ih
# BOOM
Speaking of the
RSpec doesn't, and will unlikely add any countermeasure. I'd suggest freezing what is possible, e.g. In general, there's nothing wrong with this approach, except
SOME_TEST_CONSTANT = "some_value" that would leak into the global context.
some_shared_model = FactoryBot.create(:location, name: 'New-York') A similar problem with
There is a semi-related WIP cop. I encourage you to send a PR adding a note to this style guide to be extra careful with purposed-to-be-constant variables defined on the example group level. |
I had an interesting discussion on a PR just now. Lately I've taken to using assignment to indicate things that won't change between tests:
However, there's a caveat with this. If you use a method that mutates the constant, this mutation will be persisted across future tests. This isn't the case when you use
let
, because the state of this variable will be reset between tests. However, it looks to me like variables defined withlet
will be changed between contexts, so I'm hesitant to use it for things like this, especially for fear of setting off theMultipleMemoizedVariables
cop.Any thoughts here? Does RSpec have something in its kit to address this already, or should something be introduced?
The text was updated successfully, but these errors were encountered: