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

标签 ruby-on-rails ruby ruby-on-rails-4 rspec

我正在尝试为我的 bill_total 辅助方法创建一个测试。 let 方法不生成 bill1bill2 变量。

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 语句中定义的名称仅在 beforeit block 中可用,在 describe 的外层中不可用 block 。如果您将内部的 describe 替换为 it,名称将可访问。此外,名称实际上不是变量,而是辅助方法

关于ruby-on-rails - RSpec let方法不生成变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18883660/

相关文章:

ruby-on-rails - 为什么在 ActionView::OutputBuffer 上调用 URI.escape 会失败?

ruby - &.method 语法(安全导航运算符)在 Ruby 中如何工作?

ruby-on-rails - 如何将公共(public) API 添加到 Rails 应用程序?

java - Sunspot Solr 不以 "You need a Java Runtime Environment to run the Solr server"开头

ruby-on-rails - 你怎么能让一个数字等于一个范围?

ruby-on-rails - 使用嵌套表单上传 Carrierwave?

ruby - 如何获取另一个线程的调用堆栈?

ruby-on-rails - 你如何记录到 Cucumber 中的 Rails 日志文件?

ruby-on-rails - 如何验证自定义范围事件记录 rails 4 的唯一性

ruby-on-rails - 尝试解析 XLS 文件时出现 "OLE2 signature is invalid"