Skip to content

Commit 2b4961e

Browse files
authored
Lint: Fix RSpec/MessageSpies (#1193)
1 parent a51506c commit 2b4961e

File tree

8 files changed

+107
-48
lines changed

8 files changed

+107
-48
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ RSpec/DescribedClass:
4747
- 'spec/money_spec.rb'
4848
- 'spec/money/formatting_spec.rb'
4949

50+
RSpec/MultipleExpectations:
51+
# Do not limit the amount of expectations in a spec
52+
Enabled: false
53+
5054
# Style
5155

5256
Style/ClassAndModuleChildren:

.rubocop_todo.yml

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/bank/variable_exchange_spec.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,18 @@
110110

111111
describe "#add_rate" do
112112
it 'delegates to store#add_rate' do
113-
expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25
113+
allow(bank.store).to receive(:add_rate).and_return 1.25
114114
expect(bank.add_rate('USD', 'EUR', 1.25)).to be 1.25
115+
expect(bank.store).to have_received(:add_rate).with('USD', 'EUR', 1.25)
115116
end
116117

117118
it "adds rates with correct ISO codes" do
118-
expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 0.788332676)
119+
allow(bank.store).to receive(:add_rate)
119120
bank.add_rate("USD", "EUR", 0.788332676)
121+
expect(bank.store).to have_received(:add_rate).with('USD', 'EUR', 0.788332676)
120122

121-
expect(bank.store).to receive(:add_rate).with('EUR', 'JPY', 122.631477)
122123
bank.add_rate("EUR", "YEN", 122.631477)
124+
expect(bank.store).to have_received(:add_rate).with('EUR', 'JPY', 122.631477)
123125
end
124126

125127
it "treats currency names case-insensitively" do
@@ -130,8 +132,9 @@
130132

131133
describe "#set_rate" do
132134
it 'delegates to store#add_rate' do
133-
expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25
135+
allow(bank.store).to receive(:add_rate).and_return 1.25
134136
expect(bank.set_rate('USD', 'EUR', 1.25)).to be 1.25
137+
expect(bank.store).to have_received(:add_rate).with('USD', 'EUR', 1.25)
135138
end
136139

137140
it "sets a rate" do
@@ -155,8 +158,9 @@
155158
end
156159

157160
it "delegates options to store, options are a no-op" do
158-
expect(bank.store).to receive(:get_rate).with('USD', 'EUR')
161+
allow(bank.store).to receive(:get_rate)
159162
bank.get_rate('USD', 'EUR')
163+
expect(bank.store).to have_received(:get_rate).with('USD', 'EUR')
160164
end
161165
end
162166

@@ -197,16 +201,19 @@
197201
context "with :file provided" do
198202
it "writes rates to file" do
199203
f = double('IO')
200-
expect(File).to receive(:open).with('null', 'w').and_yield(f)
201-
expect(f).to receive(:write).with(JSON.dump(@rates))
204+
allow(File).to receive(:open).with('null', 'w').and_yield(f)
205+
allow(f).to receive(:write)
202206

203207
bank.export_rates(:json, 'null')
208+
209+
expect(f).to have_received(:write).with(JSON.dump(@rates))
204210
end
205211
end
206212

207213
it "delegates execution to store, options are a no-op" do
208-
expect(bank.store).to receive(:transaction)
214+
allow(bank.store).to receive(:transaction)
209215
bank.export_rates(:yaml, nil, foo: 1)
216+
expect(bank.store).to have_received(:transaction)
210217
end
211218
end
212219

@@ -260,9 +267,10 @@
260267
end
261268

262269
it "delegates execution to store#transaction" do
263-
expect(bank.store).to receive(:transaction)
270+
allow(bank.store).to receive(:transaction)
264271
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
265272
bank.import_rates(:yaml, s, foo: 1)
273+
expect(bank.store).to have_received(:transaction)
266274
end
267275
end
268276

spec/currency/loader_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
end
77

88
it "parse currency_iso.json & currency_non_iso.json & currency_backwards_compatible.json" do
9-
expect(described_class).to receive(:parse_currency_file).exactly(3).times.and_return({})
9+
allow(described_class).to receive(:parse_currency_file).and_return({})
1010

1111
described_class.load_currencies
12+
13+
expect(described_class).to have_received(:parse_currency_file).exactly(3).times
1214
end
1315
end

spec/money/arithmetic_spec.rb

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,26 @@
140140

141141
it "converts other object amount to current currency, then compares the two object amounts (different currency)" do
142142
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"))
144144
expect(Money.new(100_00, "USD") <=> target).to be < 0
145+
expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD"))
145146

146147
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"))
148149
expect(Money.new(100_00, "USD") <=> target).to eq 0
150+
expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD"))
149151

150152
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"))
152154
expect(Money.new(100_00, "USD") <=> target).to be > 0
155+
expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD"))
153156
end
154157

155158
it "returns nil if currency conversion fails, and therefore cannot be compared" do
156159
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)
158161
expect(Money.new(100_00, "USD") <=> target).to be_nil
162+
expect(target).to have_received(:exchange_to).with(Money::Currency.new("USD"))
159163
end
160164

161165
it "can be used to compare with an object that inherits from Money" do
@@ -254,8 +258,9 @@
254258

255259
it "converts other object amount to current currency and adds other amount to current amount (different currency)" do
256260
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"))
258262
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"))
259264
end
260265

261266
it "adds Integer 0 to money and returns the same amount" do
@@ -285,8 +290,9 @@
285290

286291
it "converts other object amount to current currency and subtracts other amount from current amount (different currency)" do
287292
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"))
289294
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"))
290296
end
291297

292298
it "subtract Integer 0 to money and returns the same amount" do
@@ -422,8 +428,9 @@
422428
{ a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: 1.625 },
423429
]
424430
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))
426432
expect(t[:a] / t[:b]).to eq t[:c]
433+
expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency)
427434
end
428435
end
429436

@@ -499,8 +506,9 @@
499506
{ a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: 1.625 },
500507
]
501508
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))
503510
expect(t[:a].div(t[:b])).to eq t[:c]
511+
expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency)
504512
end
505513
end
506514

@@ -552,8 +560,9 @@
552560
{ a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: [1, Money.new(-5, :USD)] },
553561
]
554562
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))
556564
expect(t[:a].divmod(t[:b])).to eq t[:c]
565+
expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency)
557566
end
558567
end
559568

@@ -640,8 +649,9 @@
640649
{ a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: Money.new(-5, :USD) },
641650
]
642651
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))
644653
expect(t[:a].modulo(t[:b])).to eq t[:c]
654+
expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency)
645655
end
646656
end
647657
end
@@ -679,8 +689,9 @@
679689
{ a: Money.new(-13, :USD), b: Money.new(-4, :EUR), c: Money.new(-5, :USD) },
680690
]
681691
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))
683693
expect(t[:a] % t[:b]).to eq t[:c]
694+
expect(t[:b]).to have_received(:exchange_to).once.with(t[:a].currency)
684695
end
685696
end
686697
end

spec/money/formatting_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@
330330

331331
specify "(symbol: true) returns $ when currency code is not recognized" do
332332
currency = Money::Currency.new("EUR")
333-
expect(currency).to receive(:symbol).and_return(nil)
333+
allow(currency).to receive(:symbol).and_return(nil)
334334
expect(Money.new(100, currency).format(symbol: true)).to eq "¤1,00"
335335
end
336336

spec/money_spec.rb

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,20 @@
335335

336336
it "uses the correct bank inside block" do
337337
old_bank = Money.default_bank
338-
custom_store = double
338+
custom_store = double :store
339+
340+
allow(old_bank).to receive(:add_rate)
341+
allow(custom_store).to receive(:add_rate)
342+
339343
bank = Money::Bank::VariableExchange.new(custom_store)
340344
Money.with_bank(bank) do
341-
expect(custom_store).to receive(:add_rate).with("USD", "EUR", 0.5)
342345
Money.add_rate("USD", "EUR", 0.5)
343346
end
344347

345-
expect(old_bank).to receive(:add_rate).with("UAH", "NOK", 0.8)
346348
Money.add_rate("UAH", "NOK", 0.8)
349+
350+
expect(custom_store).to have_received(:add_rate).with("USD", "EUR", 0.5)
351+
expect(old_bank).to have_received(:add_rate).with("UAH", "NOK", 0.8)
347352
end
348353

349354
it 'safely handles concurrent usage in different threads' do
@@ -660,7 +665,7 @@
660665
let(:currency) { Money::Currency.new("EUR") }
661666

662667
before do
663-
expect(currency).to receive(:symbol).and_return(nil)
668+
allow(currency).to receive(:symbol).and_return(nil)
664669
end
665670

666671
it "returns a generic currency symbol" do
@@ -773,10 +778,15 @@
773778
describe "#to_money" do
774779
it "works as documented" do
775780
money = Money.new(10_00, "DKK")
781+
allow(money.bank)
782+
.to receive(:exchange_with)
783+
.and_return(Money.new(200_00, Money::Currency.new("EUR")))
776784
expect(money).to eq money.to_money
777785
expect(money).to eq money.to_money("DKK")
778-
expect(money.bank).to receive(:exchange_with).with(Money.new(10_00, Money::Currency.new("DKK")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
779786
expect(money.to_money("EUR")).to eq Money.new(200_00, "EUR")
787+
expect(money.bank)
788+
.to have_received(:exchange_with)
789+
.with(Money.new(10_00, Money::Currency.new("DKK")), Money::Currency.new("EUR"))
780790
end
781791
end
782792

@@ -802,14 +812,24 @@
802812
describe "#exchange_to" do
803813
it "exchanges the amount via its exchange bank" do
804814
money = Money.new(100_00, "USD")
805-
expect(money.bank).to receive(:exchange_with).with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
815+
allow(money.bank)
816+
.to receive(:exchange_with)
817+
.and_return(Money.new(200_00, Money::Currency.new('EUR')))
806818
money.exchange_to("EUR")
819+
expect(money.bank)
820+
.to have_received(:exchange_with)
821+
.with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR"))
807822
end
808823

809824
it "exchanges the amount properly" do
810825
money = Money.new(100_00, "USD")
811-
expect(money.bank).to receive(:exchange_with).with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
826+
allow(money.bank)
827+
.to receive(:exchange_with)
828+
.and_return(Money.new(200_00, Money::Currency.new('EUR')))
812829
expect(money.exchange_to("EUR")).to eq Money.new(200_00, "EUR")
830+
expect(money.bank)
831+
.to have_received(:exchange_with)
832+
.with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR"))
813833
end
814834

815835
it "allows double conversion using same bank" do
@@ -822,14 +842,17 @@
822842

823843
it 'uses the block given as rounding method' do
824844
money = Money.new(100_00, 'USD')
825-
expect(money.bank).to receive(:exchange_with).and_yield(300_00)
826-
expect { |block| money.exchange_to(Money::Currency.new('EUR'), &block) }.to yield_successive_args(300_00)
845+
allow(money.bank).to receive(:exchange_with).and_yield(300_00)
846+
expect { |block| money.exchange_to(Money::Currency.new('EUR'), &block) }
847+
.to yield_successive_args(300_00)
848+
expect(money.bank).to have_received(:exchange_with)
827849
end
828850

829851
it "does no exchange when the currencies are the same" do
830852
money = Money.new(100_00, "USD")
831-
expect(money.bank).not_to receive(:exchange_with)
853+
allow(money.bank).to receive(:exchange_with)
832854
expect(money.exchange_to("USD")).to eq money
855+
expect(money.bank).not_to have_received(:exchange_with)
833856
end
834857
end
835858

@@ -1071,8 +1094,15 @@
10711094
end
10721095

10731096
describe ".default_currency" do
1074-
before { Money.setup_defaults }
1075-
after { Money.setup_defaults }
1097+
before do
1098+
allow(Money).to receive(:warn)
1099+
1100+
Money.setup_defaults
1101+
end
1102+
1103+
after do
1104+
Money.setup_defaults
1105+
end
10761106

10771107
it "accepts a lambda" do
10781108
Money.default_currency = lambda { :eur }
@@ -1087,8 +1117,9 @@
10871117
it 'does not warn if the default_currency has been changed' do
10881118
Money.default_currency = Money::Currency.new(:usd)
10891119

1090-
expect(Money).not_to receive(:warn)
10911120
Money.default_currency
1121+
1122+
expect(Money).not_to have_received(:warn)
10921123
end
10931124
end
10941125

spec/rates_store/memory_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
end
1212

1313
describe 'add_rate' do
14+
let(:guard) { store.instance_variable_get(:@guard) }
15+
16+
before do
17+
allow(guard).to receive(:synchronize)
18+
end
19+
1420
it "uses a mutex by default" do
15-
expect(store.instance_variable_get(:@guard)).to receive(:synchronize)
1621
store.add_rate('USD', 'EUR', 1.25)
22+
23+
expect(guard).to have_received(:synchronize)
1724
end
1825
end
1926

@@ -35,9 +42,16 @@
3542
end
3643

3744
describe '#transaction' do
45+
let(:guard) { store.instance_variable_get("@guard") }
46+
47+
before do
48+
allow(guard).to receive(:synchronize)
49+
end
50+
3851
it 'uses mutex' do
39-
expect(store.instance_variable_get('@guard')).to receive(:synchronize)
4052
store.transaction { 1 + 1 }
53+
54+
expect(guard).to have_received(:synchronize)
4155
end
4256

4357
it 'wraps block in mutex transaction only once' do

0 commit comments

Comments
 (0)