【问题标题】:Rspec test NameError: uninitialized constant trying to tie a class into a rspec testRspec 测试 NameError:未初始化的常量试图将一个类绑定到 rspec 测试中
【发布时间】:2015-04-12 01:16:57
【问题描述】:

所以我对 ruby​​ 或 rspec 不太熟悉,我有一个 rspect 测试,这里有一个代码 sn-p :

describe 'thingMagic' do 
    let(:tester) { testClass.testing }
    it { expect(tester).to be whatever doesn't matter at this point

所以我编写了一个名为testClass 的类,其中包含一个名为testing 的方法。这是它的样子:

class testClass
  def testing
    return pass_test
  end
end

我试图弄清楚当我运行 rspec 时它在控制台中出现此错误时失败:

 Failure/Error: let(:tester) { testClass.testing }
     NameError:
       uninitialized constant testClass

我似乎不知道如何初始化这个类。是否有我必须在 rspec 中修改的文件为其提供该文件的相对路径?

【问题讨论】:

标签: ruby rspec rspec2


【解决方案1】:

经过大量的谷歌搜索和纯粹的运气。我学会了: 我需要:

require_relative '<path_to_file>'

在我将它添加到 spec.rb 文件中之后。我重新运行了 rspec 测试,它开始工作了。

这里有很好的文档,基本上让我意识到我错过了什么: http://ruby.about.com/od/faqs/qt/Nameerror-Uninitialized-Constant-Object-Something.htm

【讨论】:

  • 我对此表示赞成,因为它有效,尽管通常这是最后的手段。该错误通常意味着您的 rspec 配置缺少某些内容。
猜你喜欢
  • 2013-11-10
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 2023-04-02
  • 2014-07-16
  • 1970-01-01
相关资源
最近更新 更多