Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Preference on instance variables in tests #2

Closed
adarsh opened this Issue Aug 29, 2012 · 2 comments

Comments

Projects
None yet
3 participants
Contributor

adarsh commented Aug 29, 2012

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.

Owner

joshuaclayton commented Aug 30, 2012

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.

Owner

jferris commented Aug 30, 2012

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

@jferris jferris added a commit that referenced this issue Aug 30, 2012

@jferris jferris Avoid using instance variables on tests.
* From discussion in issue #2.
6266bbc

@jferris jferris closed this Sep 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment