Why the variable defined in 'let' block is no more accessible ?

55 views
Skip to first unread message

Javix

unread,
Nov 3, 2012, 3:54:31 PM11/3/12
to rs...@googlegroups.com
I have a very simple spec defined as follows:

describe Account do
  let(:client) { create(:client) }
  before { @account = build(:account, client: client) }

  subject { @account }
 
  its(:client) { should == client }

  it { should be_valid }
 
  it { should respond_to(:acc_number) }
  it { should respond_to(:client_id) }
  it { should respond_to(:client) }
  it { should respond_to(:operations) }

  describe "has a generated number" do
    before { @account.save }   
    its(:acc_number) { should_not be_nil }
  end

Two models are defined as follows:

Client -> has_many :accounts
Account -> belongs_to :client

At this stage all the tests are passing. Now if I add one more like that:

describe Account do
  let(:client) { create(:client) }
  before { @account = build(:account, client: client) }

  subject { @account }
 
  its(:client) { should == client }

  it { should be_valid }
 
  it { should respond_to(:acc_number) }
  it { should respond_to(:client_id) }
  it { should respond_to(:client) }
  it { should respond_to(:operations) }

  describe "has a generated number" do
    before { @account.save }   
    its(:acc_number) { should_not be_nil }
  end
 
  describe "its generated account number should be increased by 1 fro;m the latest" do
    last = client.accounts.last   
    last.should_not be_nil
  end
 #it is just to prove and show that call to 'client fails :)
It fails like that:

account_spec.rb:24:in `block (2 levels) in <top (required)>': undefined local variable or method `client' for #<Class:0x878c5a0> (NameError)
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
    from /home/serge/Development/Ruby/Rails3/jlc-invest/spec/models/account_spec.rb:23:in `block in <top (required)>'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
    from /home/serge/Development/Ruby/Rails3/jlc-invest/spec/models/account_spec.rb:3:in `<top (required)>'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:66:in `rescue in run'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:62:in `run'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

What is wring here? Why previousely defined client variable is 'visible' everywhere before but the last test? Thank you.

David Chelimsky

unread,
Nov 3, 2012, 5:15:32 PM11/3/12
to rs...@googlegroups.com
The following code is eval'd in the context of the group, which is a class. Wrap these two lines in an example (it "does something" do) and it should work as you expect.

    last = client.accounts.last   
    last.should_not be_nil
  end
 #it is just to prove and show that call to 'client fails :)
It fails like that:

account_spec.rb:24:in `block (2 levels) in <top (required)>': undefined local variable or method `client' for #<Class:0x878c5a0> (NameError)
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
    from /home/serge/Development/Ruby/Rails3/jlc-invest/spec/models/account_spec.rb:23:in `block in <top (required)>'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
    from /home/serge/Development/Ruby/Rails3/jlc-invest/spec/models/account_spec.rb:3:in `<top (required)>'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:66:in `rescue in run'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:62:in `run'
    from /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

What is wring here? Why previousely defined client variable is 'visible' everywhere before but the last test? Thank you.

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To post to this group, send email to rs...@googlegroups.com.
To unsubscribe from this group, send email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rspec/-/jfgsqqmL2BMJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Javix

unread,
Nov 3, 2012, 6:12:20 PM11/3/12
to rs...@googlegroups.com

The following code is eval'd in the context of the group, which is a class. Wrap these two lines in an example (it "does something" do) and it should work as you expect.
I put the 2 lines as follows but still have errors:

describe "has a generated number" do
    before { @account.save }   
    its(:acc_number) { should_not be_nil }
   
    context "some description comes here" do

      last = client.accounts.last  
      last.should_not be_nil
    end
  end  

Javix

unread,
Nov 3, 2012, 6:18:28 PM11/3/12
to rs...@googlegroups.com
What I really can't get is WHY 'client' defined in let block is visible before but in that block ?

David Chelimsky

unread,
Nov 3, 2012, 6:19:18 PM11/3/12
to rs...@googlegroups.com

On Nov 3, 2012, at 5:12 PM, Javix <s.ca...@gmail.com> wrote:


The following code is eval'd in the context of the group, which is a class. Wrap these two lines in an example (it "does something" do) and it should work as you expect.
I put the 2 lines as follows but still have errors:

Use "it", not "context"
To view this discussion on the web visit https://groups.google.com/d/msg/rspec/-/0YSpdCS09mEJ.

Javix

unread,
Nov 3, 2012, 6:20:15 PM11/3/12
to rs...@googlegroups.com
Sorry for the typo, NOT visible in the last block :)

David Chelimsky

unread,
Nov 3, 2012, 6:25:16 PM11/3/12
to rs...@googlegroups.com
Different scopes. The blocks passed to describe and context are eval'd in the scope of the example group (class). The blocks passed to let, subject, it, before, and after are eval'd in the scope of an instance of that class.

HTH

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To post to this group, send email to rs...@googlegroups.com.
To unsubscribe from this group, send email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rspec/-/2FPfIqC680sJ.

Javix

unread,
Nov 4, 2012, 7:45:26 AM11/4/12
to rs...@googlegroups.com
Thank you, David, for this explication, it works for me.

moger777

unread,
Nov 3, 2012, 5:04:34 PM11/3/12
to rspec
Your test needs to be within an it statement. You are doing it within
a describe, change the last part to:

it "should increase it's account number by 1 from the latest" do
last = client.accounts.last
last.should_not be_nil
end

> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/e xample_group.rb:224:in
> `describe'
>     from
> /home/serge/Development/Ruby/Rails3/jlc-invest/spec/models/account_spec.rb: 23:in
> `block in <top (required)>'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/e xample_group.rb:238:in
> `module_eval'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/e xample_group.rb:238:in
> `subclass'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/e xample_group.rb:224:in
> `describe'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/d sl.rb:18:in
> `describe'
>     from
> /home/serge/Development/Ruby/Rails3/jlc-invest/spec/models/account_spec.rb: 3:in
> `<top (required)>'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/c onfiguration.rb:780:in
> `load'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/c onfiguration.rb:780:in
> `block in load_spec_files'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/c onfiguration.rb:780:in
> `map'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/c onfiguration.rb:780:in
> `load_spec_files'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/c ommand_line.rb:22:in
> `run'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/r unner.rb:66:in
> `rescue in run'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/r unner.rb:62:in
> `run'
>     from
> /home/serge/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.11.1/lib/rspec/core/r unner.rb:8:in
Reply all
Reply to author
Forward
0 new messages