Skip to content

Differentiating between test constants and context variables #127

Open
@boardfish

Description

@boardfish

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:

describe Something do
  # This won't change between contexts, but we want to reuse it between tests
  some_test_constant = "some_value"

  # This will change between contexts
  let(:weather) { "sunny" }
end

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 with let will be changed between contexts, so I'm hesitant to use it for things like this, especially for fear of setting off the MultipleMemoizedVariables cop.

Any thoughts here? Does RSpec have something in its kit to address this already, or should something be introduced?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions