i have an array of raw_responses using web_mock i want use them as args of to_return method and chain to stub_request method:

#["file1.txt", "subfolder/file_n.txt", "awsome_name.txt"]
rr = Dir.glob(File.expand_path("../../markups/*.txt", __FILE__))
stub_request(:get, "www.google.de").to_return(rr[0]).to_return(rr[1]).to_return(rr[2])

How to rewrite last string using enumerator or something like tap to use all array of rr?

有帮助吗?

解决方案

As described in the README, you simply pass the multiple responses to the to_return method as arguments. Basically you want something like:

stub_request(:get, "www.google.de").to_return(rr[0], rr[1], rr[2])

But listing the array values like that is a bit clunky (and won't work with a variable number of responses), so you should instead use Ruby's splat operator:

responses = Dir[File.expand_path("../../markups/*.txt", __FILE__)].map{|f| File.read(f)}
stub_request(:get, "www.google.de").to_return(*responses)

其他提示

finaly i did it like this

stub = stub_request(:get, "www.google.de")
rr.each { |rf| stub.tap { |s| s.to_return(File.new(rf)) } }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top