![](/img/trans.png)
[英]Rspec how to checks the value present in the an another method which that method doesn't return anything?
[英]How to write a stub with RSpec for a Rails method which calls a block and doesn't return value?
我在控制器中定義了以下函數:
def respond_with(action = 0, &block)
if block_given?
response = get_response
block.call( MyResponse.new(response) )
end
end
這樣我就可以查詢響應對象:
respond_with do |response|
case response.status
when 'ok'
when 'errors'
#etc etc...
end
如果需要,我可以毫無限制地調用它。
如果我有一個返回值的函數,它將類似於
controller.should_receive(:respond_with).with(DO_SOMETHING).and_return(something)
可以的
def respond_with(action = 0)
...
response
end
如何檢查| response |中是否使用特定值調用了該塊 ?
代替
.and_return(something)
我用:
.and_yield(api_response_mock)
做到了。 並且由於該塊獲得了響應對象,因此gem rspec-mocks在這里很方便。
您可以使用https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/yield-matchers#yield-with-args-matcher中所述的“收益匹配器”
例如,您可以這樣做:
expect {|b| controller.respond_with(&b)}.to yield_with_args(MyResponse)
如果您只想檢查是否使用MyResponse
參數調用了該塊一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.