如何使用rspec/rspec-mocks对类方法进行存根 [英] How to stub a class method using rspec/rspec-mocks

查看:87
本文介绍了如何使用rspec/rspec-mocks对类方法进行存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rspec-mock进行测试驱动的开发. 我开始实现单个类,并使用rspec-mock模拟/存根其他类. 尚未实现的类的模拟对象效果很好. 但是,当我尝试模拟尚不存在的类的类方法时,我没有成功. 我的类哈希"应具有一个类方法"calculate_hashes",该方法接收文件名并返回哈希.

I am using rspec-mock for test-driven-development. I am starting implementing a single class and mocking/stubbing the other classes using rspec-mock. Mocking objects of classes yet to be implemented works well. However when I try to mock a class method of a class that does not exist yet, I haven't been successful. My class "Hashes" should have a class method "calculate_hashes" receiving a filename and returning a hash.

我尝试过

 allow(Hashes).to receive(:calculate_hash) do |file| 
      # looks up what to return
 end

给出错误哈希不是类". 然后,我实现了一个哈希"类

which give the error "Hashes is not a class". I then implemented a class "Hashes"

class Hashes
end

,然后仅尝试以相同方式对类方法进行存根. 这给出了错误哈希未实现:calculate_hash" 然后将方法添加到类定义中时:

and then only tried to stub the class method in the same way. This gives the error "Hashes does not implement: calculate_hash" When I then add the method to my class definition:

class Hashes
    def self.calculate_hash(filename)
    end
end

它终于可以工作了,我的此类方法的桩头工作使用了"allow(Hashes)",如上例所示. 我只是想知道是否有一种无需编写此类框架即可完成此任务的方法.

it finally works and my stubbing of this class method works using "allow(Hashes)" as seen in the example above. I just wonder if there is a way of accomplishing this without writing this class skeleton.

还是我可能试图以不合适的方式完成某件事? 还是rspec-mock可能不是执行此操作的正确工具?

Or am I maybe trying to accomplish something in an inappropriate way? Or is rspec-mock maybe not the right tool to do this?

非常感谢您的帮助.

推荐答案

对于您的工作流程,我认为使用class_double比直接存根Hashes类要更好. allow(Hashes)总是 ,要求定义Hashes常量.这只是Ruby的工作方式,而RSpec对此无能为力.使用 class double ,您可以这样做:

For your workflow, I think it's going to work better to use a class_double than than to stub the Hashes class directly. allow(Hashes) is always going to require that the Hashes constant is defined. It's simply how Ruby works and RSpec can't do anything about that. With a class double, you can instead do this:

class_double("Hashes", :calculate_hash => canned_return_value).as_stubbed_const

# or

hashes = class_double("Hashes").as_stubbed_const
allow(hashes).to receive(:calculate_hash) do |file|
  # look up what to return
end

class_double("Hashes")为您提供了一个测试双项,当定义了Hashes常量时,它将对照Hashes类定义来验证模拟方法和存根方法,但是当未定义该方法时,其作用就像普通double,允许在其上嘲笑或添加任何内容. as_stubbed_const位告诉rspec-mocks在示例过程中对Hashes常量进行存根,以便对Hashes的任何引用都使您的类成为双精度类,而不是真正的Hashes类,即使Hashes类也是如此从未定义.

class_double("Hashes") provides you with a test double that, when the Hashes constant is defined, will verify the mocked and stubbed methods against the Hashes class definition, but when it is not defined, will act just like a normal double that allows anything to be mocked or stubbed on it. The as_stubbed_const bit tells rspec-mocks to stub the Hashes constant for the duration of the example so that any references to Hashes get your class double rather than the real Hashes class, even if the Hashes class has never been defined.

这篇关于如何使用rspec/rspec-mocks对类方法进行存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆