On Nov 3, 2012, at 2:54 PM, Javix <[email protected]> wrote:

> 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

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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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.
>  
>  

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to