我的Rails项目中有一个gem,非常简单,它将ip地址转换为真实地址(例如Gem.locate '127.0.0.1'
返回{ country: 'de' }
代码就像这样
ip_locator / lib / service.rb
module IpLocator
class Service
include Singleton
attr_reader :db
def initialize
@db = MaxMindDB.new('db/GeoLite2-City.mmdb')
end
def locate(ip)
db.lookup ip
end
end
end
当我进入rails项目时,我可以正确地执行此操作:IpLocator::Service.instance.locate '74.125.225.224'
并获取我需要的信息。
现在我正在尝试为gem添加一些测试(不在rails项目中)。
require 'spec_helper'
RSpec.describe IpLocator do
describe 'ip_locator' do
context 'when the data exists' do
let(:ip) { '74.125.225.224' }
subject do
IpLocator::Service.instance.locate ip
end
it 'return country data' do
expect(subject.found).to be_truthy
end
end
end
end
但是当我运行测试时,我得到了
1) IpLocator ip_locator when the data exists return country data
Failure/Error: IpLocator::Service.instance.locate ip
NameError:
uninitialized constant IpLocator::Service
我已尝试将IpLocator::Service
更改为Service
,我在规范文件中尝试了ip_locator
(尽管spec_helper.rb
中已经需要{}}。我很确定我做的事情非常愚蠢但不确定究竟是什么
这是我的spec_helper.rb
require 'bundler/setup'
Bundler.setup
require 'byebug'
require 'ip_locator'
Dir[File.dirname(__FILE__) + '/lib/*.rb'].each { |file| require file }
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
我看到了错误:
NameError: uninitialized constant IpLocator::Service::Singleton
Did you mean? SignalException
然后,如果我在spec_helper中尝试要求singleton
,我会得到
1) IpLocator::Service ip_locator when the data exists return country data
Failure/Error: @db = ::MaxMindDB.new('db/GeoLite2-City.mmdb')
NameError:
uninitialized constant MaxMindDB
我疯了