lundi 25 janvier 2016

How can I test this method with mocks?

I have a instance method in a model, it implements an extarnal API, how can I test this with mocks?

the relation: A user has many amortizations

def create_amortizations!
   #I need mock it
   amortizations = ZleClient.get_amortizations({UserId: self.username, ReferenceId: self.app_id })

   amortizations.each do |amortization|
     self.amortizations.create(date_payment: amortization[:date_payment].to_date)
   end
end

the test:

describe User, :type => :model do
   it "#create_amortizations" do
      user = create(:user_with_profile, op_role: '1')
      allow(ZleClient).to receive(:get_amortizations).with(any_args).and_return({ date_payment: "11/02/2016" })

      #How can I set the amortizations variable? and access it with rspec

    end
end

Aucun commentaire:

Enregistrer un commentaire