Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

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)) } }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top