Before(:each) <- need some clarification

i am setting up a few objects that are interrelated for use in an rspec
test…

something like:

describe Dimension do

before(:each) do
text = “string here”
end

it “should puts string” do
puts text
end

end

when i run this, i get an error: undefined local variable or method
`text’

am doing something wrong?

thanks!

On 28/04/11 1:25 AM, Sergio R. wrote:

thanks!

Sergio,

You’ll want to create text as an instance variable (’@text’).

The other option is to use ‘let’ as in:

let (:text) { "string here" }

Either option will get you what you want.

Thanks,

Srushti

Your variable “text” doesn’t exist outside the before(:each) loop.
Replacing
text with @text should fix your problem.

– Bouke

2011/4/27 Sergio R. [email protected]

On Wed, Apr 27, 2011 at 2:55 PM, Sergio R. [email protected]
wrote:

Yeah, you have to use a class variable:

before(:each) do
@text = “string here”
end

it “should puts string” do
puts @text
end

Sergio,

I believe you need to make text an instance variable:

@text = "string here"

Or you could use a let block:

let(:text) do
  "string "here"
end

Then you could continue to reference the text variable as you do now.

Jon Homan

On Apr 27, 2011, at 3:55 PM, Sergio R. wrote:

it “should puts string” do
puts text
end

end

when i run this, i get an error: undefined local variable or method
`text’

am doing something wrong?

text is a local variable, not an instance variable, and is not exposed
outside the scope of the block in which it is defined. It works with an
instance variable:

describe Dimension do

before(:each) do
@text = “string here”
end

it “should puts string” do
puts @text
end

end

Make sense?

Cheers,
David

Wow! I guess this thread beated the record for simultaneous answers! :smiley:

Best regards!

Rodrigo.

Em 27-04-2011 16:55, Sergio R. escreveu:

Hi,

On 27 Apr 2011, at 20:55, Sergio R. wrote:

i am setting up a few objects that are interrelated for use in an rspec
test…

something like:

describe Dimension do

before(:each) do
text = “string here”

This defines a local variable ‘text’ which lives for the duration of the
before block. It can’t be seen by the #it block (example) below, because
it’s gone out of scope.

am doing something wrong?

thanks!

What people don’t realise is that #describe just creates an instance of
a class, called ExampleGroup. Think of the ‘before’ block and the ‘it’
blocks as methods on that class. If you defined a private variable in
one method on a class, you wouldn’t expect to be able to see it from
another one, would you?

Here’s what we used to do in this kind of situation:

describe Dimension do
  before(:each) do
    @text = "string here"
  end

  it "should puts string" do
    puts @text
  end
end

Because, as I expect you know, instance variables can be seen between
methods on a class. However, some people found the instance variables
noisy, or wanted to lazy initialise them, so started doing this:

describe Dimension do
  def text
    @text ||= "string here"
  end

  it "should puts string" do
    puts text
  end
end

As I said, #describe just creates a class, so you can define methods on
that class if you like. Now our puts statement is calling the #text
method that we’ve defined.

This is such a common pattern, that in RSpec 2, David introduced #let
blocks, which let you refactor the above code to look like this:

describe Dimension do
  let(:text) { "string here" }

  it "should puts string" do
    puts text
  end
end

Make sense?

cheers,
Matt


Freelance programmer & coach
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne

On Apr 27, 2011, at 3:55 PM, Sergio R. wrote:

thanks!

You just need to make it an instance variable instead of a local
variable.
Try @text instead of text.

wow! thanks so much for the great info!

this is exactly what i needed!

thanks again!