|
140 | 140 |
|
141 | 141 | it "converts other object amount to current currency, then compares the two object amounts (different currency)" do |
142 | 142 | target = Money.new(200_00, "EUR") |
143 | | - expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(300_00, "USD")) |
| 143 | + allow(target).to receive(:exchange_to).and_return(Money.new(300_00, "USD")) |
144 | 144 | expect(Money.new(100_00, "USD") <=> target).to be < 0 |
| 145 | + expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD")) |
145 | 146 |
|
146 | 147 | target = Money.new(200_00, "EUR") |
147 | | - expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(100_00, "USD")) |
| 148 | + allow(target).to receive(:exchange_to).and_return(Money.new(100_00, "USD")) |
148 | 149 | expect(Money.new(100_00, "USD") <=> target).to eq 0 |
| 150 | + expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD")) |
149 | 151 |
|
150 | 152 | target = Money.new(200_00, "EUR") |
151 | | - expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(99_00, "USD")) |
| 153 | + allow(target).to receive(:exchange_to).and_return(Money.new(99_00, "USD")) |
152 | 154 | expect(Money.new(100_00, "USD") <=> target).to be > 0 |
| 155 | + expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD")) |
153 | 156 | end |
154 | 157 |
|
155 | 158 | it "returns nil if currency conversion fails, and therefore cannot be compared" do |
156 | 159 | target = Money.new(200_00, "EUR") |
157 | | - expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_raise(Money::Bank::UnknownRate) |
| 160 | + allow(target).to receive(:exchange_to).and_raise(Money::Bank::UnknownRate) |
158 | 161 | expect(Money.new(100_00, "USD") <=> target).to be_nil |
| 162 | + expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD")) |
159 | 163 | end |
160 | 164 |
|
161 | 165 | it "can be used to compare with an object that inherits from Money" do |
|
254 | 258 |
|
255 | 259 | it "converts other object amount to current currency and adds other amount to current amount (different currency)" do |
256 | 260 | other = Money.new(90, "EUR") |
257 | | - expect(other).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(9_00, "USD")) |
| 261 | + allow(other).to receive(:exchange_to).and_return(Money.new(9_00, "USD")) |
258 | 262 | expect(Money.new(10_00, "USD") + other).to eq Money.new(19_00, "USD") |
| 263 | + expect(other).to have_received(:exchange_to).with(Money::Currency.new("USD")) |
259 | 264 | end |
260 | 265 |
|
261 | 266 | it "adds Integer 0 to money and returns the same amount" do |
|
285 | 290 |
|
286 | 291 | it "converts other object amount to current currency and subtracts other amount from current amount (different currency)" do |
287 | 292 | other = Money.new(90, "EUR") |
288 | | - expect(other).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(9_00, "USD")) |
| 293 | + allow(other).to receive(:exchange_to).and_return(Money.new(9_00, "USD")) |
289 | 294 | expect(Money.new(10_00, "USD") - other).to eq Money.new(1_00, "USD") |
| 295 | + expect(other).to have_received(:exchange_to).with(Money::Currency.new("USD")) |
290 | 296 | end |
291 | 297 |
|
292 | 298 | it "subtract Integer 0 to money and returns the same amount" do |
|
422 | 428 | { a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: 1.625 }, |
423 | 429 | ] |
424 | 430 | ts.each do |t| |
425 | | - expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD)) |
| 431 | + allow(t[:b]).to receive(:exchange_to).and_return(Money.new(t[:b].cents * 2, :USD)) |
426 | 432 | expect(t[:a] / t[:b]).to eq t[:c] |
| 433 | + expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency) |
427 | 434 | end |
428 | 435 | end |
429 | 436 |
|
|
499 | 506 | { a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: 1.625 }, |
500 | 507 | ] |
501 | 508 | ts.each do |t| |
502 | | - expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD)) |
| 509 | + allow(t[:b]).to receive(:exchange_to).and_return(Money.new(t[:b].cents * 2, :USD)) |
503 | 510 | expect(t[:a].div(t[:b])).to eq t[:c] |
| 511 | + expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency) |
504 | 512 | end |
505 | 513 | end |
506 | 514 |
|
|
552 | 560 | { a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: [1, Money.new(-5, :USD)] }, |
553 | 561 | ] |
554 | 562 | ts.each do |t| |
555 | | - expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD)) |
| 563 | + allow(t[:b]).to receive(:exchange_to).and_return(Money.new(t[:b].cents * 2, :USD)) |
556 | 564 | expect(t[:a].divmod(t[:b])).to eq t[:c] |
| 565 | + expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency) |
557 | 566 | end |
558 | 567 | end |
559 | 568 |
|
|
640 | 649 | { a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: Money.new(-5, :USD) }, |
641 | 650 | ] |
642 | 651 | ts.each do |t| |
643 | | - expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD)) |
| 652 | + allow(t[:b]).to receive(:exchange_to).and_return(Money.new(t[:b].cents * 2, :USD)) |
644 | 653 | expect(t[:a].modulo(t[:b])).to eq t[:c] |
| 654 | + expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency) |
645 | 655 | end |
646 | 656 | end |
647 | 657 | end |
|
679 | 689 | { a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: Money.new(-5, :USD) }, |
680 | 690 | ] |
681 | 691 | ts.each do |t| |
682 | | - expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD)) |
| 692 | + allow(t[:b]).to receive(:exchange_to).and_return(Money.new(t[:b].cents * 2, :USD)) |
683 | 693 | expect(t[:a] % t[:b]).to eq t[:c] |
| 694 | + expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency) |
684 | 695 | end |
685 | 696 | end |
686 | 697 | end |
|
0 commit comments