Skip to content

Commit 3d523b1

Browse files
authored
Lint: Fix RSpec subject cops (#1190)
1 parent 894e538 commit 3d523b1

File tree

9 files changed

+111
-123
lines changed

9 files changed

+111
-123
lines changed

.rubocop_todo.yml

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

spec/bank/base_spec.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Money::Bank::Base do
4+
subject(:bank) { described_class.new }
5+
46
describe ".instance" do
57
it "is local to one class" do
68
subclass = Class.new(described_class)
@@ -35,43 +37,44 @@ def setup
3537

3638
describe "#exchange_with" do
3739
it "is not implemented" do
38-
expect { subject.exchange_with(Money.new(100, 'USD'), 'EUR') }.to raise_error(NotImplementedError)
40+
expect { bank.exchange_with(Money.new(100, 'USD'), 'EUR') }.to raise_error(NotImplementedError)
3941
end
4042
end
4143

4244
describe "#same_currency?" do
4345
it "accepts str/str" do
44-
expect { subject.send(:same_currency?, 'USD', 'EUR') }.not_to raise_error
46+
expect { bank.send(:same_currency?, 'USD', 'EUR') }.not_to raise_error
4547
end
4648

4749
it "accepts currency/str" do
48-
expect { subject.send(:same_currency?, Money::Currency.wrap('USD'), 'EUR') }.not_to raise_error
50+
expect { bank.send(:same_currency?, Money::Currency.wrap('USD'), 'EUR') }.not_to raise_error
4951
end
5052

5153
it "accepts str/currency" do
52-
expect { subject.send(:same_currency?, 'USD', Money::Currency.wrap('EUR')) }.not_to raise_error
54+
expect { bank.send(:same_currency?, 'USD', Money::Currency.wrap('EUR')) }.not_to raise_error
5355
end
5456

5557
it "accepts currency/currency" do
56-
expect { subject.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR')) }.not_to raise_error
58+
expect { bank.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR')) }.not_to raise_error
5759
end
5860

5961
it "returns true when currencies match" do
60-
expect(subject.send(:same_currency?, 'USD', 'USD')).to be true
61-
expect(subject.send(:same_currency?, Money::Currency.wrap('USD'), 'USD')).to be true
62-
expect(subject.send(:same_currency?, 'USD', Money::Currency.wrap('USD'))).to be true
63-
expect(subject.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('USD'))).to be true
62+
expect(bank.send(:same_currency?, 'USD', 'USD')).to be true
63+
expect(bank.send(:same_currency?, Money::Currency.wrap('USD'), 'USD')).to be true
64+
expect(bank.send(:same_currency?, 'USD', Money::Currency.wrap('USD'))).to be true
65+
expect(bank.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('USD'))).to be true
6466
end
6567

6668
it "returns false when currencies do not match" do
67-
expect(subject.send(:same_currency?, 'USD', 'EUR')).to be false
68-
expect(subject.send(:same_currency?, Money::Currency.wrap('USD'), 'EUR')).to be false
69-
expect(subject.send(:same_currency?, 'USD', Money::Currency.wrap('EUR'))).to be false
70-
expect(subject.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR'))).to be false
69+
expect(bank.send(:same_currency?, 'USD', 'EUR')).to be false
70+
expect(bank.send(:same_currency?, Money::Currency.wrap('USD'), 'EUR')).to be false
71+
expect(bank.send(:same_currency?, 'USD', Money::Currency.wrap('EUR'))).to be false
72+
expect(bank.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR'))).to be false
7173
end
7274

7375
it "raises an UnknownCurrency error when an unknown currency is passed" do
74-
expect { subject.send(:same_currency?, 'AAA', 'BBB') }.to raise_error(Money::Currency::UnknownCurrency)
76+
expect { bank.send(:same_currency?, 'AAA', 'BBB') }
77+
.to raise_error(Money::Currency::UnknownCurrency)
7578
end
7679
end
7780
end

spec/bank/single_currency_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Money::Bank::SingleCurrency do
4+
subject(:bank) { described_class.new }
5+
46
describe "#exchange_with" do
57
it "raises when called" do
68
expect do
7-
subject.exchange_with(Money.new(100, 'USD'), 'EUR')
9+
bank.exchange_with(Money.new(100, "USD"), "EUR")
810
end.to raise_error(Money::Bank::DifferentCurrencyError, "No exchanging of currencies allowed: 1.00 USD to EUR")
911
end
1012
end

spec/bank/variable_exchange_spec.rb

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'yaml'
55

66
RSpec.describe Money::Bank::VariableExchange do
7+
subject(:bank) { described_class.new }
8+
79
describe "#initialize" do
810
context "without &block" do
911
let(:bank) do
@@ -108,87 +110,87 @@
108110

109111
describe "#add_rate" do
110112
it 'delegates to store#add_rate' do
111-
expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25
112-
expect(subject.add_rate('USD', 'EUR', 1.25)).to be 1.25
113+
expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25
114+
expect(bank.add_rate('USD', 'EUR', 1.25)).to be 1.25
113115
end
114116

115117
it "adds rates with correct ISO codes" do
116-
expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 0.788332676)
117-
subject.add_rate("USD", "EUR", 0.788332676)
118+
expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 0.788332676)
119+
bank.add_rate("USD", "EUR", 0.788332676)
118120

119-
expect(subject.store).to receive(:add_rate).with('EUR', 'JPY', 122.631477)
120-
subject.add_rate("EUR", "YEN", 122.631477)
121+
expect(bank.store).to receive(:add_rate).with('EUR', 'JPY', 122.631477)
122+
bank.add_rate("EUR", "YEN", 122.631477)
121123
end
122124

123125
it "treats currency names case-insensitively" do
124-
subject.add_rate("usd", "eur", 1)
125-
expect(subject.get_rate('USD', 'EUR')).to eq 1
126+
bank.add_rate("usd", "eur", 1)
127+
expect(bank.get_rate('USD', 'EUR')).to eq 1
126128
end
127129
end
128130

129131
describe "#set_rate" do
130132
it 'delegates to store#add_rate' do
131-
expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25
132-
expect(subject.set_rate('USD', 'EUR', 1.25)).to be 1.25
133+
expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25
134+
expect(bank.set_rate('USD', 'EUR', 1.25)).to be 1.25
133135
end
134136

135137
it "sets a rate" do
136-
subject.set_rate('USD', 'EUR', 1.25)
137-
expect(subject.store.get_rate('USD', 'EUR')).to eq 1.25
138+
bank.set_rate('USD', 'EUR', 1.25)
139+
expect(bank.store.get_rate('USD', 'EUR')).to eq 1.25
138140
end
139141

140142
it "raises an UnknownCurrency error when an unknown currency is passed" do
141-
expect { subject.set_rate('AAA', 'BBB', 1.25) }.to raise_error(Money::Currency::UnknownCurrency)
143+
expect { bank.set_rate('AAA', 'BBB', 1.25) }.to raise_error(Money::Currency::UnknownCurrency)
142144
end
143145
end
144146

145147
describe "#get_rate" do
146148
it "returns a rate" do
147-
subject.set_rate('USD', 'EUR', 1.25)
148-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
149+
bank.set_rate('USD', 'EUR', 1.25)
150+
expect(bank.get_rate('USD', 'EUR')).to eq 1.25
149151
end
150152

151153
it "raises an UnknownCurrency error when an unknown currency is passed" do
152-
expect { subject.get_rate('AAA', 'BBB') }.to raise_error(Money::Currency::UnknownCurrency)
154+
expect { bank.get_rate('AAA', 'BBB') }.to raise_error(Money::Currency::UnknownCurrency)
153155
end
154156

155157
it "delegates options to store, options are a no-op" do
156-
expect(subject.store).to receive(:get_rate).with('USD', 'EUR')
157-
subject.get_rate('USD', 'EUR')
158+
expect(bank.store).to receive(:get_rate).with('USD', 'EUR')
159+
bank.get_rate('USD', 'EUR')
158160
end
159161
end
160162

161163
describe "#export_rates" do
162164
before do
163-
subject.set_rate('USD', 'EUR', 1.25)
164-
subject.set_rate('USD', 'JPY', 2.55)
165+
bank.set_rate('USD', 'EUR', 1.25)
166+
bank.set_rate('USD', 'JPY', 2.55)
165167

166168
@rates = { "USD_TO_EUR" => 1.25, "USD_TO_JPY" => 2.55 }
167169
end
168170

169171
context "with format == :json" do
170172
it "returns rates formatted as json" do
171-
json = subject.export_rates(:json)
173+
json = bank.export_rates(:json)
172174
expect(JSON.load(json)).to eq @rates
173175
end
174176
end
175177

176178
context "with format == :ruby" do
177179
it "returns rates formatted as ruby objects" do
178-
expect(Marshal.load(subject.export_rates(:ruby))).to eq @rates
180+
expect(Marshal.load(bank.export_rates(:ruby))).to eq @rates
179181
end
180182
end
181183

182184
context "with format == :yaml" do
183185
it "returns rates formatted as yaml" do
184-
yaml = subject.export_rates(:yaml)
186+
yaml = bank.export_rates(:yaml)
185187
expect(YAML.load(yaml)).to eq @rates
186188
end
187189
end
188190

189191
context "with unknown format" do
190192
it "raises Money::Bank::UnknownRateFormat" do
191-
expect { subject.export_rates(:foo) }.to raise_error Money::Bank::UnknownRateFormat
193+
expect { bank.export_rates(:foo) }.to raise_error Money::Bank::UnknownRateFormat
192194
end
193195
end
194196

@@ -198,79 +200,82 @@
198200
expect(File).to receive(:open).with('null', 'w').and_yield(f)
199201
expect(f).to receive(:write).with(JSON.dump(@rates))
200202

201-
subject.export_rates(:json, 'null')
203+
bank.export_rates(:json, 'null')
202204
end
203205
end
204206

205207
it "delegates execution to store, options are a no-op" do
206-
expect(subject.store).to receive(:transaction)
207-
subject.export_rates(:yaml, nil, foo: 1)
208+
expect(bank.store).to receive(:transaction)
209+
bank.export_rates(:yaml, nil, foo: 1)
208210
end
209211
end
210212

211213
describe "#import_rates" do
212214
context "with format == :json" do
213215
it "loads the rates provided" do
214216
s = '{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}'
215-
subject.import_rates(:json, s)
216-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
217-
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
217+
bank.import_rates(:json, s)
218+
expect(bank.get_rate('USD', 'EUR')).to eq 1.25
219+
expect(bank.get_rate('USD', 'JPY')).to eq 2.55
218220
end
219221
end
220222

221223
context "with format == :ruby" do
222224
let(:dump) { Marshal.dump({ "USD_TO_EUR" => 1.25, "USD_TO_JPY" => 2.55 }) }
223225

224226
it "loads the rates provided" do
225-
subject.import_rates(:ruby, dump)
227+
bank.import_rates(:ruby, dump)
226228

227-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
228-
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
229+
expect(bank.get_rate('USD', 'EUR')).to eq 1.25
230+
expect(bank.get_rate('USD', 'JPY')).to eq 2.55
229231
end
230232

233+
# rubocop:disable RSpec/SubjectStub
231234
it "prints a warning" do
232-
allow(subject).to receive(:warn)
235+
allow(bank).to receive(:warn)
233236

234-
subject.import_rates(:ruby, dump)
237+
bank.import_rates(:ruby, dump)
235238

236-
expect(subject)
239+
expect(bank)
237240
.to have_received(:warn)
238241
.with(include('[WARNING] Using :ruby format when importing rates is potentially unsafe'))
239242
end
243+
# rubocop:enable RSpec/SubjectStub
240244
end
241245

242246
context "with format == :yaml" do
243247
it "loads the rates provided" do
244248
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
245-
subject.import_rates(:yaml, s)
246-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
247-
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
249+
bank.import_rates(:yaml, s)
250+
expect(bank.get_rate('USD', 'EUR')).to eq 1.25
251+
expect(bank.get_rate('USD', 'JPY')).to eq 2.55
248252
end
249253
end
250254

251255
context "with unknown format" do
252256
it "raises Money::Bank::UnknownRateFormat" do
253-
expect { subject.import_rates(:foo, "") }.to raise_error Money::Bank::UnknownRateFormat
257+
expect { bank.import_rates(:foo, "") }
258+
.to raise_error Money::Bank::UnknownRateFormat
254259
end
255260
end
256261

257262
it "delegates execution to store#transaction" do
258-
expect(subject.store).to receive(:transaction)
263+
expect(bank.store).to receive(:transaction)
259264
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
260-
subject.import_rates(:yaml, s, foo: 1)
265+
bank.import_rates(:yaml, s, foo: 1)
261266
end
262267
end
263268

264269
describe "#marshal_dump" do
265270
it "does not raise an error" do
266-
expect { Marshal.dump(subject) }.not_to raise_error
271+
expect { Marshal.dump(bank) }.not_to raise_error
267272
end
268273

269274
it "works with Marshal.load" do
270-
bank = Marshal.load(Marshal.dump(subject))
275+
new_bank = Marshal.load(Marshal.dump(bank))
271276

272-
expect(bank.rates).to eq subject.rates
273-
expect(bank.rounding_method).to eq subject.rounding_method
277+
expect(new_bank.rates).to eq bank.rates
278+
expect(new_bank.rounding_method).to eq bank.rounding_method
274279
end
275280
end
276281
end

spec/currency/loader_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
RSpec.describe Money::Currency::Loader do
44
it "returns a currency table hash" do
5-
expect(subject.load_currencies).to be_a Hash
5+
expect(described_class.load_currencies).to be_a Hash
66
end
77

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

11-
subject.load_currencies
11+
described_class.load_currencies
1212
end
1313
end
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Money::LocaleBackend::Currency do
4+
subject(:backend) { described_class.new }
5+
46
describe '#lookup' do
57
let(:currency) { Money::Currency.new('EUR') }
68

79
it 'returns thousands_separator as defined in currency' do
8-
expect(subject.lookup(:thousands_separator, currency)).to eq('.')
10+
expect(backend.lookup(:thousands_separator, currency)).to eq('.')
911
end
1012

1113
it 'returns decimal_mark based as defined in currency' do
12-
expect(subject.lookup(:decimal_mark, currency)).to eq(',')
14+
expect(backend.lookup(:decimal_mark, currency)).to eq(',')
1315
end
1416

1517
it 'returns format based as defined in currency' do
16-
expect(subject.lookup(:format, currency)).to be_nil
18+
expect(backend.lookup(:format, currency)).to be_nil
1719
end
1820
end
1921
end

0 commit comments

Comments
 (0)