2015-02-12 18 views
14

Tôi đã tạo một ứng dụng đường ray mới và làm theo hướng dẫn cài đặt cho rspec-ray tại đây - https://github.com/rspec/rspec-rails Sau đó tôi đã tạo (sao chép từ interwebs) mô-đun sau trong thư mục app/lib của mình.hằng số uninitialized Rspec

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 

Sau đó, tôi đã tạo tệp thông số sau.

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 

Khi tôi cố gắng chạy spec tôi nhận được lỗi sau

/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. 

Tôi không chắc chắn những gì tôi đang làm sai ở đây. Bất cứ ai có thể cung cấp một số cái nhìn sâu sắc về những gì tôi có thể thử? Cảm ơn!

Trả lời

36

Từ lỗi:

uninitialized constant Rspec (NameError) 

Trong đặc tả Cipher bạn, bạn đã sai chính tả RSpec như Rspec. Số nhận dạng Ruby phân biệt chữ hoa chữ thường và bạn chưa xác định Rspec, do đó lỗi.

+3

Oh geeze, tôi không thể tin rằng mình đã bỏ lỡ điều đó. Cảm ơn! –

+0

@geoffswartz - ok ... chỉ cần * Chính xác * điều tương tự;) – Nick

Các vấn đề liên quan