Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Preference on instance variables in tests #2

Closed
adarsh opened this Issue · 2 comments

3 participants

@adarsh
Admin

I generally don't use instance variables in tests - I believe this was part of Josh's TDD Workshop curricula but I'm not sure.

Is there a stance on this? If so, should go into the testing section.

@joshuaclayton

Instance variables should be avoided except in very specific circumstances (LevelUp, while using Test::Unit + shoulda, had an explicit style which recommends instance variables, for example).

The only state that would possibly carry over in RSpec would be data structures created in the before block; with that in mind, I try to minimize the amount of code written in before blocks and instead build up state per test. This means a faster test suite (typically) because some tests may not need EVERY piece of data generated in the test setup. Additionally, it allows for more explicit data creation, many times through a DSL, making it a lot easier to read.

In the Intro to Test-Driven Rails workshop, I recommend against instance variables in steps for a number of reasons. First, steps which are already coupled to each other based on application state are additionally coupled when you start using instance variables. The steps I've seen using ivars end up doing something along the lines of @ivar ||= something or adding additional steps to set instance variables manually. Second, if your steps don't currently use ivars, adding them typically requires additional work to convert existing steps or set them when necessary. Third, it encourages better step writing (Given I am signed in when you don't care about the user's email vs Given I am signed in as "josh@thoughtbot.com" when email does matter in the scenario) by being explicit about the data that you care about. Most data can be referred to by a pseudo-PK (for a user, it's email; for a todo, it's title), so I tend to focus on creating data with that explicitly, but only when I need to refer back to it.

I feel the same way about ivars in Cucumber steps as I do in RSpec integration tests - instance variables can be used there to set up state and write really sexy methods that refer to the variables, but I think that the issues I mentioned with Cucumber steps carry through here as well. Generally, I try to avoid mutation as much as possible in all aspects of my code, specs and features included.

@jferris
Admin

The current style guide already advises against using before blocks. I'd also advise against using instance variables.

@jferris jferris referenced this issue from a commit
@jferris jferris Avoid using instance variables on tests.
* From discussion in issue #2.
6266bbc
@jferris jferris closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.