未初始化的常数Rspec

时间:2015-02-12 21:20:22

标签: rspec-rails

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

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

然后我创建了以下spec文件。

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.

我不确定我在这里做错了什么。任何人都可以提供一些我可以尝试的见解吗?谢谢!

3 个答案:

答案 0 :(得分:47)

从错误:

uninitialized constant Rspec (NameError)

在您的密码规范中,您将RSpec拼错为Rspec。 Ruby标识符区分大小写,您还没有定义Rspec,因此错误。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

运行RSpec时收到uninitiated constant错误,因为我有一些孤立的测试,没有相应的控制器或模型。控制器\模型在此过程中被删除,但是它们的测试仍然悬而未决。删除孤立的测试,RSpec再次正确运行。