我正在尝试为我的bill_total
辅助方法创建一个测试。 let方法不生成bill1
和bill2
变量。
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
错误:
/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]
答案 0 :(得分:13)
let
语句中定义的名称仅在before
和it
块中可用,而不在describe
块的外层。如果您将内部describe
替换为it
,则可以访问这些名称。此外,名称实际上不是变量,而是helper methods
。
答案 1 :(得分:2)
您需要在it
块内编写示例,而不是describe
块:
it "#bill_total" do
bill1.amount = 1.00
bill2.amount = 1.00
expect(bills_helper.bill_total).to eq(2.00)
end