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! -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2011-Apr-27 20:46 UTC
[rspec-users] before(:each) <- need some clarification
On Apr 27, 2011, at 3:55 PM, Sergio Ruiz 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" > 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?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
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 Wed, Apr 27, 2011 at 2:55 PM, Sergio Ruiz <lists at ruby-forum.com> 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" > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > 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/20110427/87ad8433/attachment.html>
Bouke Woudstra
2011-Apr-27 20:51 UTC
[rspec-users] before(:each) <- need some clarification
Your variable "text" doesn''t exist outside the before(:each) loop. Replacing text with @text should fix your problem. -- Bouke 2011/4/27 Sergio Ruiz <lists at ruby-forum.com>> 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > 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/20110427/1c228b72/attachment.html>
Srushti Ambekallu
2011-Apr-27 20:58 UTC
[rspec-users] before(:each) <- need some clarification
On 28/04/11 1:25 AM, Sergio Ruiz 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" > 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! >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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110428/616a7a70/attachment.html>
On Wed, Apr 27, 2011 at 2:55 PM, Sergio Ruiz <lists at ruby-forum.com> 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" > 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? >Yeah, you have to use a class variable: before(:each) do @text = "string here" end it "should puts string" do puts @text end> > thanks! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > 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/20110427/7b20f941/attachment.html>
Hi, On 27 Apr 2011, at 20:55, Sergio Ruiz 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.> 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!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
Rodrigo Rosenfeld Rosas
2011-Apr-27 22:06 UTC
[rspec-users] before(:each) <- need some clarification
Wow! I guess this thread beated the record for simultaneous answers! :D Best regards! Rodrigo. Em 27-04-2011 16:55, Sergio Ruiz escreveu:> 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! >
wow! thanks so much for the great info! this is exactly what i needed! thanks again! -- Posted via http://www.ruby-forum.com/.
Matthew Van Horn
2011-Apr-28 17:36 UTC
[rspec-users] before(:each) <- need some clarification
On Apr 27, 2011, at 3:55 PM, Sergio Ruiz 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" > 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!You just need to make it an instance variable instead of a local variable. Try @text instead of text.