我有2个项目,把我所造成拉动其他模块,像这样一个大模块“公共代码”项目:红宝石:如何包括外模块Rspec的测试
这里的文件夹结构“我的常见项目”:
- 我的常见项目
- 共同
- file_utils.rb
- 休息,client.rb
- 其他Ruby文件与模块...
- common.rb
- 的Gemfile
- 等...
- 共同
common.rb
require 'bundler'
Bundler.require
require_relative './common/file-utils.rb'
require_relative './common/rest_client.rb'
...
module Common
include FileUtils
include RestClient
# include other modules here...
file_utils.rb
module Common
module FileUtils
def open_file(file_name)
dir = File.expand_path('') << '/lib'
FileUtils.mkdir_p(dir) unless File.directory?(dir)
File.open(File.expand_path('') << "/lib/#{file_name}", 'w')
end
end
end
我也有一个RSpec项目中,我做了一个试验:
- 我-rspec的项目
- 规范
- my_test_spec.rb
- spec_helper.rb
- LIB
- my_class.rb
- .rspec
- 的Gemfile
- 等...
- 规范
spec_helper.rb
require 'bundler'
require 'csv'
require_relative './lib/fp_relationship_api'
require_relative './../../../../../RubyProjects/mksta-common/common'
Bundler.require
RSpec.configure do |config|
...
config.include Common
...
end
my_class.rb
require "#{File.expand_path('') << '/spec_helper'}"
class MyClass
include Common
@error_file = open_file('error_file.txt')
...
end
我得到的错误:
undefined method `open_file' for MyClass:Class (NoMethodError)
有人能看到什么地方出了错?
'open_file'defined在哪里? – BernardK
@BernardK,open_file是在公共项目中的另一个文件的类中定义的。我会更新这个问题。谢谢 –