Ruby on rails 初始化Ruby模块

Ruby on rails 初始化Ruby模块,ruby-on-rails,ruby,Ruby On Rails,Ruby,尝试学习Ruby及以下是我为了测试IO Ruby特性而创建的一个模块。当我运行以下测试时: subject{TestGem.new} it 'should be_a Module' do subject.is_a Module end it 'creates a config file' do subject.init_config File.open("program.config", "r") { |io| i

尝试学习Ruby及以下是我为了测试IO Ruby特性而创建的一个模块。当我运行以下测试时:

    subject{TestGem.new}    

    it 'should be_a Module' do
      subject.is_a Module
    end 

    it 'creates a config file' do
      subject.init_config
      File.open("program.config", "r") { |io| io.readline }.should eq "default_file.txt"    
    end 
对于以下两种情况,我都会遇到此错误:

 Failure/Error: subject{TestGem.new}
 NoMethodError:
   undefined method `new' for TestGem:Module
这是我正在测试的模块。如有任何帮助/建议,将不胜感激:)

$LOAD\u PATH.unshift File.expand\u PATH(“../test\u gem”,\uuuuu File\uuu)
需要“版本”
需要“你好”
模块TestGem
@默认文件名
@支持的类型
def初始化
default\u file\u name='default\u file.txt'
支持的_类型=['txt','pdf']
结束
将“模块TestGem已定义”
def self.init_config
File.open('program.config',“w”)do | f|
f、 写入(yaml(@default_file_name))
f、 写入(yaml(@支持的_类型))
结束
结束
类MyFile
def self.first(文件名)
File.open(文件名,“r”){| f | f.readline}
结束
def self.last(文件名)
打开(文件名为“r”)[-1]
结束
结束
类MyError<标准错误
放入“标准错误”
结束
结束

简短回答:您不能实例化模块对象

module A
end

class B
end

B.methods - A.methods #=> [:new, :superclass, :allocate]
要测试模块,您可以将其包含在如下对象中

  object = Object.new
  object.extend(TestGem)

或者,如果模块依赖于某些类行为,则可以创建一些示例类。

简短回答:您不能实例化模块对象

module A
end

class B
end

B.methods - A.methods #=> [:new, :superclass, :allocate]
要测试模块,您可以将其包含在如下对象中

  object = Object.new
  object.extend(TestGem)

或者,如果您的模块依赖于某些类行为,您可以创建一些示例类。

您将如何为它们编写rspec测试?或者,或者,
klass=class.new{include TestGem};object=klass.new
@AndrewMarshall是的,完全忘了你还可以包含模块而不是扩展。感谢诸如此类的案例(通常,在开发的初始阶段,因为规范并不是很有用),我通常只去
subject{Module}
然后
指定{subject.should_a Module}
。另外,您可以将模块直接包含到规范中,只需开始调用
self
上的方法,您将如何为它们编写rspec测试?或者,或者,
klass=Class.new{include TestGem};object=klass.new
@AndrewMarshall是的,完全忘了你还可以包含模块而不是扩展。感谢诸如此类的案例(通常,在开发的初始阶段,因为规范并不是很有用),我通常只去
subject{Module}
然后
指定{subject.should_a Module}
。此外,您还可以将模块直接包含到规范中,只需在
self