如何强制Rspec不要错误地认为父模块中嵌套了另一个模块?

时间:2019-07-01 16:55:03

标签: ruby rspec jwt

我有一个问题仅在运行rspec时出现,我的应用程序在本地和生产环境中加载并正常工作

rspec似乎认为宝石JWT属于我的模块BACKBONE

An error occurred while loading spec_helper.
Failure/Error: JWT.encode(payload, ENV['APP_SECRET'], 'HS256')

NameError:
  uninitialized constant BACKBONE::JWT
# ./lib/jwt.rb:6:in `encode'
# ./lib/backbone/policies.rb:7:in `<class:Policy>'
# ./lib/backbone/policies.rb:5:in `<module:BACKBONE>'
# ./lib/backbone/policies.rb:3:in `<top (required)>'
# ./init.rb:10:in `load'
# ./init.rb:10:in `block in <module:CrewManagement>'
# ./init.rb:10:in `glob'
# ./init.rb:10:in `<module:CrewManagement>'
# ./init.rb:4:in `<top (required)>'
# ./spec/spec_helper.rb:16:in `require'
# ./spec/spec_helper.rb:16:in `<top (required)>'

文件按加载到内存的顺序列出,可能有必要注意,我还有其他几个具有相同设置的应用程序,都没有遇到此问题

/spec/spec_helper.rb

require 'bundler'
require 'simplecov'
SimpleCov.start

Bundler.require :default
Dotenv.load("./.env.#{(ENV['RACK_ENV'] || 'development')}")

require 'rack/test'
require './init'

# ...

/init.rb

module CrewManagement
  ## ...
  Dir.glob('./lib/**/*.rb') { |file| load file }
  ## ...
end

/lib/jwt.rb

module BACKBONE
  def self.encode(payload)
    JWT.encode(payload, ENV['APP_SECRET'], 'HS256')
  end

  def self.decode(token)
    JWT.decode(token, ENV['APP_SECRET'], true, algorithm: 'HS256')
  end
end

/lib/backbone/policies.rb

module BACKBONE
  headers = { 'Authorization' => BACKBONE.encode(app: '...') }
end

0 个答案:

没有答案