require 'spec_helper' describe ActiveRemote::Bulk do shared_examples_for "a bulk method" do |bulk_method| let(:records) { [ Hash.new ] } let(:response) { double(:response, :records => []) } before { Tag.rpc.better_stub(:execute).and_return(response) } context "given an empty array" do let(:parsed_records) { { :records => records } } let(:records) { [] } it "calls #{bulk_method} with parsed records" do Tag.rpc.better_receive(:execute).with(bulk_method, parsed_records) Tag.__send__(bulk_method, records) end end context "given an array of record hashes" do let(:hash_record){ double(:record, :to_hash => {}) } let(:parsed_records) { { :records => records.map(&:to_hash) } } let(:records) { [ hash_record ] } it "calls #{bulk_method} with parsed records" do Tag.rpc.better_receive(:execute).with(bulk_method, parsed_records) Tag.__send__(bulk_method, records) end end context "given an array of remote records" do let(:parsed_records) { { :records => records.map(&:attributes) } } let(:records) { [ remote_record ] } let(:remote_record) { double(:remote, :attributes => {}) } it "calls #{bulk_method} with parsed records" do Tag.rpc.better_receive(:execute).with(bulk_method, parsed_records) Tag.__send__(bulk_method, records) end end context "given a bulk message" do let(:parsed_records) { { :records => records.map(&:to_hash) } } let(:tag) { Generic::Remote::Tag.new } let(:tags) { Generic::Remote::Tags.new(:records => [ tag ]) } it "calls #{bulk_method} with parsed records" do Tag.rpc.better_receive(:execute).with(bulk_method, parsed_records) Tag.__send__(bulk_method, tags) end end end describe ".create_all" do it_behaves_like "a bulk method", :create_all end describe ".delete_all" do it_behaves_like "a bulk method", :delete_all end describe ".destroy_all" do it_behaves_like "a bulk method", :destroy_all end describe ".update_all" do it_behaves_like "a bulk method", :update_all end end