spec/lib/percentable/percent_spec.rb in percentable-1.0.2 vs spec/lib/percentable/percent_spec.rb in percentable-1.1.2
- old
+ new
@@ -1,8 +1,9 @@
require 'spec_helper'
require 'bigdecimal'
require 'percentable'
+require 'money'
describe Percentable::Percent do
subject { Percentable::Percent.new(value) }
let(:value) { nil }
@@ -90,18 +91,10 @@
end
end
end
describe '#coerce' do
- context 'when other is non numeric' do
- let(:other) { 'string' }
-
- it 'should raise a TypeError' do
- expect { subject.coerce(other) }.to raise_error TypeError
- end
- end
-
context 'math' do
let(:value) { 50 }
it 'should add the value of percent*number' do
expect(10 + subject).to eq 15
@@ -221,27 +214,42 @@
it 'should return the result of multipling the percent by the number' do
expect(percent * numeric).to eq subject.class.new(40)
end
end
- context 'multiplying anything else' do
+ context 'multiplying something that responds to coerce' do
let(:other_thing) { double }
- let(:percent) { subject.class.new(20) }
+ let(:value) { 20 }
- context 'responds to to_f' do
- before { allow(other_thing).to receive(:to_f).and_return 1.0 }
+ before { allow(other_thing).to receive(:respond_to?).with(:to_f).and_return(false) }
+ before { allow(other_thing).to receive(:respond_to?).with(:coerce).and_return(true) }
- it "should return the float value times the percent" do
- expect(percent * other_thing).to eq subject.class.new(20)
- end
+ it 'should call coerce on the other object' do
+ expect(other_thing).to receive(:coerce).with(subject).and_return([0, 1])
+ subject * other_thing
end
- context 'does not respond to to_f' do
- before { allow(other_thing).to receive(:to_f).and_raise(NoMethodError) }
+ it 'should multiply the things that are returned from coerce' do
+ expect(other_thing).to receive(:coerce).with(subject).and_return([0, 1])
+ expect(subject * other_thing).to eq 0 * 1
+ end
+ end
- it "should raise a no method error" do
- expect { percent * other_thing }.to raise_error(NoMethodError)
- end
+ context 'multiplying against Money' do
+ let(:value) { 50 }
+ let(:money) { Money.new(100) }
+
+ it "should return 50% of the money" do
+ expect(subject * money).to eq Money.new(50)
+ end
+ end
+
+ context "multiplying against an Integer" do
+ let(:value) { 50 }
+ let(:integer) { 10 }
+
+ it "should return 50% of the number" do
+ expect(subject * integer).to eq 5
end
end
end
describe '#/' do