[rspec-users] Problem with before(:all)

Wilker wilkerlucio at gmail.com
Fri Jan 21 09:24:22 EST 2011


if you wanna share variables from before filters to specs, you need to use
instance variables instead of local ones, example:

WRONG:

before :all do
  myvar = 1
end

it "should do something" do
  puts myvar # nil
end

RIGHT:

before :all do
  @myvar = 1
end

it "should do something" do
  puts @myvar # 1
end
---
Wilker Lúcio
http://about.me/wilkerlucio/bio
Kajabi Consultant
+55 81 82556600



On Fri, Jan 21, 2011 at 11:12 AM, Rich Price <rich at gandalf.ws> wrote:

> I have a module with two methods for marshaling objects.
> The following rspec code is used to test this module:
>
> -----  Start Code  -----
> require '../code/fmutil.rb'
> require '../code/fmgrid.rb'
> require '../code/serial.rb'
>
> # start test of Serial Module
> describe Serial do
>  describe "Given a grid with the block test pattern" do
>    before(:all) do
>      cfg01 = Fmconfig.new
>      grid01 = Grid.makeempty(cfg01,500,20)
>      cfg01.list[:cellhight].times do |y|
>        cfg01.list[:cellwidth].times do |x|
>          grid01[x,y].elevation = x+10*(y+1)
>        end
>      end
>    end
>    describe "restored serialized grid object" do
>      it "matches origional grid object." do
>        grid02 = Grid.allocate
>        Serial.write(grid01,"testtemp01")
>        grid02 = Serial.read(grid02,"testtemp01")
>        grid01[9,9].elevation.should == grid02[9,9].elevation
>      end
>    end
>  end
> end
> -----  End Code  -----
>
> But when I run it it seems that the Grid object created in the
> before(:all) hook is not available to the example [see below].
> Why is this?
>
> -----  Start Run  -----
> rich at richlaptop2:~/Documents/fakemap/rspec$ rspec serialgrid.rb
>
> Serial
>  Given a grid with the block test pattern
>    restored serialized grid object
>      matches origional grid object. (FAILED - 1)
>
> Failures:
>
>  1) Serial Given a grid with the block test pattern restored serialized
> grid object matches origional grid object.
>     Failure/Error: Serial.write(grid01,"testtemp01")
>     NameError:
>       undefined local variable or method `grid01' for
> #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0xb7677a0c>
>     # ./serialgrid.rb:20
>
> Finished in 0.00698 seconds
> 1 example, 1 failure
> rich at richlaptop2:~/Documents/fakemap/rspec$
> -----  End Run  -----
>
>
> I am running rspec 2.4.0 and ruby 1.8.7
>
>
> --
> Rich Price
> rich at gandalf.ws
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110121/cf6d271d/attachment.html>


More information about the rspec-users mailing list