RSpec — test if method called its block parameter

大憨熊 提交于 2019-12-01 20:02:09
rogerdpack

I usually do something like

a = 1
b.go { a = 2}
a.should == 2

I like using throw instead of raise for this sort of problem, because it can't be rescued be an arbitrary rescue handler. So it might look like this:

my_proc = proc { throw :my_proc_was_called }
expect {
  my_proc.call
}.to throw_symbol :my_proc_was_called

Thanks to Dave Newton's suggestion in the comment above I did something like this:

 it "should run block defining the node" do
   message="This message is raised if block is properly evaluated."
   expect do
     node do
       raise message
     end 
   end.to raise_error message
 end

In case of error this prints message:

 Failure/Error: expect do
   expected Exception with "This message is raised if block is properly evaluated." but nothing was raised

Which I find informative enough.

Thanks again for help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!