简体   繁体   English

Rspec-类内部模块

[英]Rspec - Module inside class

I have tried several way to rspec the 'to_type' function. 我尝试了几种方法来指定'to_type'函数。 The fact that it is inside the class means that only the class should be able to call it right? 它在类内部的事实意味着只有该类才能调用它吗? I've tried to include the Class in my rspec but the module "Format" is still not recognized. 我试图在我的rspec中包括该类,但仍无法识别模块“格式”。 Any ideas how I can rspec this method 'to_type' from the module? 有什么想法可以从模块中重新指定此方法“ to_type”吗?

class Loom::Lma < Loom::Base
  module Format
    STANDARD_FORMATS = {
      1 => '0',
      2 => '13.4',
    }
    def to_type(format)
      # type is calculated here then return type
      # for instance
      return :date
    end
    module_function :to_type
  end
  def initialize()
    #init stuff
  end
  def otherstuff()
        #another function
  end
end

RSPEC RSPEC

it 'type should not be :date' do
   include Loom::Lma
   Format.to_type('some string format').should_not eq(:date)
end

Any ideas? 有任何想法吗?

Are you sure you want to put that module into a class not the other way around? 您确定要将该模块放到一个类中吗? Anyway, you can access to_type like this: 无论如何,您可以像这样访问to_type:

Loom::Lma::Format.to_type()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM