Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec let method not generating variable

I'm trying to create a test for my bill_total helper method. The let method isn't generating the bill1 and bill2 variable.

describe BillsHelper do
  let(:bill1) { Bill.create(name: 'Bill 1', amount: 1.00) }
  let(:bill2) { Bill.create(name: 'Bill 2', amount: 1.00) }

  describe "#bill_total" do
    bill1.amount = 1.00
    bill2.amount = 1.00

    expect(bills_helper.bill_total).to eq(2.00)
  end
end

Error:

    /Users/adrianleeelder/Documents/Developer_Resources/sites/bills_app/spec/helpers/bills_helper_spec.rb:18:in `block (2 levels) in <top (required)>': undefined local variable or method `bill1' for #<Class:0x007fb3c121b548> (NameError)
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `module_eval'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `subclass'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe'
    from /Users/adrianleeelder/Documents/Developer_Resources/sites/bills_app/spec/helpers/bills_helper_spec.rb:17:in `block in <top (required)>'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `module_eval'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `subclass'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/dsl.rb:18:in `describe'
    from /Users/adrianleeelder/Documents/Developer_Resources/sites/bills_app/spec/helpers/bills_helper_spec.rb:13:in `<top (required)>'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
    from /Users/adrianleeelder/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
[Finished in 2.4s with exit code 1]
like image 282
Adrian Elder Avatar asked Sep 18 '13 22:09

Adrian Elder


2 Answers

The names defined in let statements are only available within before and it blocks, not within the outer level of the describe blocks. If you replace your inner describe with it, the names will be accessible. Also, the names are not actually variables, but helper methods.

like image 134
Peter Alfvin Avatar answered Nov 10 '22 10:11

Peter Alfvin


You need to write your examples inside it blocks, not describe blocks:

it "#bill_total" do
  bill1.amount = 1.00
  bill2.amount = 1.00

  expect(bills_helper.bill_total).to eq(2.00)
end 
like image 26
Ryan Bigg Avatar answered Nov 10 '22 12:11

Ryan Bigg