简体   繁体   English

如何测试 Rspec 中的简单块参数?

[英]How to test a simple block argument in Rspec?

I am trying to write an expectation for this snippet of code:我正在尝试为这段代码写一个期望:

Sidekiq::Queue.all.each(&:clear)

I know the shorthand generates a block and so far I have tried the satisfy matcher, and this is the current version I am working with.我知道速记会生成一个块,到目前为止我已经尝试过满足匹配器,这是我正在使用的当前版本。 I know this syntax is incorrect, and I will keep updating what I try:我知道这种语法不正确,我会不断更新我尝试的内容:

expect(Sidekiq::Queue).to receive_message_chain(:all, :each) { &:clear }.and_return(true)

This is the approach I used to solve this but I am open to other ideas on HOW not WHY as testing blocks can be tough, and I think this would be a great example.这是我用来解决这个问题的方法,但我对其他关于HOW而不是WHY的想法持开放态度,因为测试块可能很困难,我认为这将是一个很好的例子。

expect(Sidekiq::Queue).to receive_message_chain(:all, :each) do |&block|
  expect(block).to be(Proc.new(&:clear))
end

Credit where credit is due: https://github.com/rspec/rspec-mocks/issues/1182信用到期的信用: https://github.com/rspec/rspec-mocks/issues/1182

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

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