首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rspec -测试用类名调用模块方法的类方法

Rspec -测试用类名调用模块方法的类方法
EN

Stack Overflow用户
提问于 2017-04-30 14:26:12
回答 1查看 846关注 0票数 0

我想编写rspec测试来验证一个类方法,它通过类名调用包含的模块方法。当我使用module调用模块方法时,它工作得很好,但在通过类名调用时抛出NoMethodError。

代码语言:javascript
复制
module Test
  def self.module_mtd
    p "test"
  end
end

class Burger 
  include Test

  attr_reader :options

  def initialize(options={})
    @options = options
  end

  def apply_ketchup
    @ketchup = @options[:ketchup]
  end

  def has_ketchup_on_it?
    Burger.module_mtd # Throws NoMethodError
    Test.module_mtd #Works fine as expected
    @ketchup
  end
end


describe Burger do
  describe "#apply_ketchup" do
    subject { burger }
    before  { burger.apply_ketchup }

    context "with ketchup" do
      let(:burger) { Burger.new(:ketchup => true) }

      it { should have_ketchup_on_it }
    end

    context "without ketchup" do
      let(:burger) { Burger.new(:ketchup => false) }

      it { should_not have_ketchup_on_it }
    end
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-30 14:48:13

问题不在于测试本身,而在于您对类方法如何在Ruby中工作的理解。

代码语言:javascript
复制
module Test
  def self.module_mtd
    p "test"
  end
end

声明属于Test的方法。与模块实例方法不同,当您包含模块时,这个方法不会添加到类中。

要从模块中声明类方法,需要使用模块混合模式来扩展单例类:

代码语言:javascript
复制
module Test
  # this is called when you include the module
  def self.included(base)
    # this adds the class methods to the class
    base.extend ClassMethods
  end

  module ClassMethods
    def foo
      puts "hello world"
    end
  end
end

module ClassMethodsfoo声明为实例方法的事实似乎有点令人困惑,直到您意识到正在扩展的单例类是" class“的实例。

请参见:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43707472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档