【问题标题】:RSpec testing module methods/RSpec 测试模块方法/
【发布时间】:2012-06-13 14:26:42
【问题描述】:

我有下一个模块。

module A
  module B      
    def self.method_b(value)
      #code
    end
  end 
end

module A
  module C
    def self.method_c(value)
       A::B.method_b(value)
    end
  end
end

如何测试这些模块?如何创建存根 self.method_b?

spec_helper.rb

RSpec.configure do |config|
 config.mock_with :mocha
end

谢谢。

【问题讨论】:

  • A::B.stub(:method_b).and_return("whatever")
  • NoMethodError:undefined method 'stub' for A::B:Module
  • 对我来说似乎是 RSpec 中的一个错误。 #stub 应该适用于所有对象:-\
  • 看看我的spec_helper.rb。可能需要在 spec_helper 中添加一些东西?

标签: ruby rspec bdd stub


【解决方案1】:

您已经关闭了 mocha 的内置 RSpec 模拟库。那有一个不同的API。试试:

A::B.stubs(:method_b).returns('value')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多