From: Douglas Seifert Date: 2011-06-19T10:17:19+09:00 Subject: Re: test scope issue --00151747358626284204a606617d Content-Type: text/plain; charset=ISO-8859-1 Each test method gets a new @life instance because the setup method runs before each test. The theory is each test should run without side affects from other tests. I am no testing expert either, but I would write your test in one method. def test_vivify_and_murder @life.vivify(2,-1) assert( @life.world[2][-1] ) @life.murder(2,-1) assert( !@life.world[2][-1] ) end Again, I'm sure this is considered poor style and not pure TDD/BDD. -Doug On Sat, Jun 18, 2011 at 5:30 PM, Chad Perrin wrote: > My tests are broken. I generally suck at writing tests, so please bear > with me. > > require 'test/unit' > require '../life.rb' > > class TestLife < Test::Unit::TestCase > def setup > @life = Life.new > end > > def test_life_exists > assert( @life ) > end > > def test_vivify_vivifies > @life.vivify(2,-1) > assert( @life.world[2][-1] ) > end > > def test_murder_kills > @life.murder(2,-1) > assert( !@life.world[2][-1] ) > end > end > > I seem to be having a scoping issue here, where the changes made in > test_vivify_vivifies are not applying to @life in the general case; they > go away after test_vivify_vivifies ends, so that test_murder_kills errors > out because @life.world[2] doesn't exist at all at that point. > > I have verified that when I run the code in life.rb using irb, the > @life.vivify actually gives me what I expect, and @life.murder thus works > perfectly well there. Actually, I don't even use the @ for an instance > variable when doing that; I only included it here to avoid scoping issues > with local variables, but I seem to be having different scoping issues as > a result. It so far seems to be only when running tests that there is an > error. > > I'm probably doing something stupid, but I have not yet seen it. > > -- > Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] > --00151747358626284204a606617d--