简体   繁体   English

minitest stub_any_instance和方法?

[英]Minitest stub_any_instance and methods?

If I do 如果我做

Klass.stub_any_instance(:new, raise(RuntimeError) do
  ...
end

the RuntimeError is raised at the stub_any_instance line and not, as I would like, later when a Klass.new() occurs. 在stub_any_instance行处引发RuntimeError,而不是我稍后希望在Klass.new()发生时引发。

Is there a way to make this work the way I would like? 有没有一种方法可以按照我的意愿进行这项工作?

Wrap the raise in a lambda: raise换成lambda:

Klass.stub :new, -> { raise(RuntimeError) } do
  assert_raise { Klass.new }
end

(You'll also want to use stub rather than stub_any_instance .) (您还将想要使用stub而不是stub_any_instance 。)

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

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