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
On Nov 3, 3: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
> 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/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/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
> `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].
For more options, visit https://groups.google.com/groups/opt_out.