I have this method:
def download_zip_file
FileUtils.rm_rf(@zip_path) if @zip_path
FileUtils.mkdir_p(@zip_path)
downloaded_file = File.open(@zip_file, "wb")
request = Typhoeus::Request.new(@feed, followlocation: true)
request.on_body { |chunk| downloaded_file.write(chunk) }
request.on_complete { |response| downloaded_file.close }
request.run
end
It clears the zip_path, recreates it, opens a file for writing, then downloads the file from the @feed URL and writes to the downloaded file in chunks.
I'm wondering how to unit test it, mocking the actual request. Since it uses chunking via some blocks, it's a bit complicated.
I formerly had this code:
def download_feed_data_from_url
response = Typhoeus.get(@feed, followlocation: true)
raise(FeedNotFoundError, "Could not find feed at feed: #{@feed}. Response: #{response.inspect}") unless response.success?
result = response.body
end
Which was easy to test (by mocking Typhoeus and providing a stub return):
context "testing feed downloading" do
let(:feed) { "http://the.feed.url" }
let(:response) { double(body: "some content", success?: true) }
before
allow(Typhoeus).to receive(:get).with(feed, followlocation:true).and_return(response)
end
# ... individual assertions, i.e. that Typhoeus is called, that it pulls the body content, etc.
end
So I'm wondering how to unit test the same kinds of things... i.e. that the the path is created, the file is saved, etc. while mocking Typhoeus. Since it's a 3rd party library, I don't need to test that it works, just that it's called correctly.
It's the chunking and on_body and on_complete that are confusing me (in terms of how to test it)
Aucun commentaire:
Enregistrer un commentaire