加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RSpec让方法不生成变量

发布时间:2020-12-16 21:40:28 所属栏目:百科 来源:网络整理
导读:我正在尝试为我的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"
我正在尝试为我的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]

解决方法

let语句中定义的名称仅在之前和之后可用,而不是在describe块的外部级别内.如果用它替换内部描述,则可以访问名称.此外,名称实际上不是变量,而是辅助方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读