diff --git a/lib/facebook_ads/batch_api/batch.rb b/lib/facebook_ads/batch_api/batch.rb index ab70a636..6be7edce 100644 --- a/lib/facebook_ads/batch_api/batch.rb +++ b/lib/facebook_ads/batch_api/batch.rb @@ -32,7 +32,7 @@ def <<(api_req) def execute return [] if operations.empty? - operations.each_slice(50) do |slice| + operations.each_slice(50).map do |slice| api_response = APIRequest.new(:post, '', session: session, params: batch_args(slice)).execute_now self.last_api_response = api_response slice.zip(api_response.result).map do |req, res| diff --git a/spec/facebook_ads/batch_api/batch_spec.rb b/spec/facebook_ads/batch_api/batch_spec.rb new file mode 100644 index 00000000..9fed94ee --- /dev/null +++ b/spec/facebook_ads/batch_api/batch_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +RSpec.describe FacebookAds::Batch do + describe "#execute" do + let(:expected_api_response) do + status = 200 + headers = { content_type: "application/json" } + body = [ + { + "code" => status, + "headers": headers.map { |k, v| { name: k, value: v } }, + "body" => { "id" => "act_123456", "name" => "test account name" } + } + ].to_json + FacebookAds::APIResponse.new(status, headers, body) + end + before do + expect_any_instance_of(FacebookAds::APIRequest).to receive(:execute_now).and_return(expected_api_response) + end + + it do + batch = FacebookAds::Batch.with_batch do + FacebookAds::AdAccount.get("act_123456").name + end + batch_api_responses = batch.execute + api_responses = batch_api_responses.first + api_response = api_responses.first + + expect(api_response).to be_a FacebookAds::AdAccount + expect(api_response).to match have_attributes(id: "act_123456", name: "test account name") + end + end +end