【问题标题】:uninitialized constant Rspec未初始化的常数 Rspec
【发布时间】:2015-04-13 19:25:21
【问题描述】:

我创建了一个新的 rails 应用程序并按照此处的 rspec-rails 安装说明 - https://github.com/rspec/rspec-rails 然后在我的 app/lib 目录中创建(从互联网复制)以下模块。

require 'openssl'
require 'base64'

module Cipher
  def self.encrypt(key, data)
    data += 'A' # Add 'A' suffix to support empty data
    cipher(:encrypt, key, data)
  end

  def self.decrypt(key, text)
    data = cipher(:decrypt, key, text)
    data[0...-1] # Remove the 'A' suffix
  end

  def self.encrypt_base64(key, data)
    blowfish_string = self.encrypt(key, data)
    Base64.encode64(blowfish_string)
  end

  def self.decrypt_base64(key, base64_string)
    blowfish_string = Base64.decode64(base64_string)
    self.decrypt(key, blowfish_string)
  end

  private

  def self.cipher(mode, key, data)
    cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').send(mode)
    cipher.key = Digest::SHA256.digest(key)
    cipher.update(data) << cipher.final
  end
end

然后我创建了以下规范文件。

require 'rails_helper'

Rspec.describe Ciper do

  describe "cipher encrypts data" do
    let(:key) { 'secret key' }

    it "encrypts a string" do
      original = ''
      encrypted = Cipher.encrypt(key, original)
      decrypted = Cipher.decrypt(key, encrypted)
      expect(decrypted).to equal original
    end
  end

end

当我尝试运行规范时,我收到以下错误

/Users/user/RubymineProjects/skeleton/spec/lib/cipher_spec.rb:3:in `<top (required)>': uninitialized constant Rspec (NameError)
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1226:in `load'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1224:in `each'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:97:in `setup'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:85:in `run'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:70:in `run'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/user/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.0/exe/rspec:4:in `<top (required)>'
    from /Users/user/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load'
    from /Users/user/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1
Empty test suite.

我不确定我在这里做错了什么。谁能提供一些我可以尝试的见解?谢谢!

【问题讨论】:

    标签: rspec-rails


    【解决方案1】:

    来自错误:

    uninitialized constant Rspec (NameError)
    

    在您的密码规范中,您将RSpec 拼错为Rspec。 Ruby 标识符区分大小写,并且您尚未定义 Rspec,因此会出现错误。

    【讨论】:

    • 天哪,我不敢相信我错过了。谢谢!
    • @geoffswartz - 没关系...只是完全做了同样的事情;)
    • 几个月后放弃说“一样!”
    【解决方案2】:

    我收到此错误是因为 ruby​​mine 试图运行“测试”而不是“rspecs”。当我将运行配置更改为运行 RSpec 而不是 Test 时,一切正常。

    【讨论】:

    • 运行 > 编辑配置 > + 并添加“RSpec”,然后进入您的 Specs 文件夹。
    【解决方案3】:

    我在运行 RSpec 时收到了 uninitiated constant 错误,因为我有一些没有相应控制器或模型的孤立测试。控制器\模型在途中的某个地方被删除了,但他们的测试仍然存在。删除了孤立的测试,RSpec 再次正确运行。

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多