简体   繁体   中英

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.

Is there a way to make this work the way I would like?

Wrap the raise in a lambda:

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

(You'll also want to use stub rather than stub_any_instance .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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