Skip to content

Commit 1da5f35

Browse files
committed
Add RSpec tests and shared contexts for monetizable attributes and currency detection
1 parent fba9d0e commit 1da5f35

File tree

10 files changed

+583
-394
lines changed

10 files changed

+583
-394
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
require_relative 'money_helpers'
6+
7+
if defined? ActiveRecord
8+
describe MoneyRails::ActiveRecord::Monetizable do
9+
include MoneyHelpers
10+
11+
describe "#currency_for" do
12+
context "when detecting currency based on different conditions" do
13+
it "detects currency based on instance currency name" do
14+
product = Product.new(sale_price_currency_code: 'CAD')
15+
currency = product.send(:currency_for, :sale_price, :sale_price_currency_code, nil)
16+
17+
expect_to_be_a_currency_instance(currency)
18+
expect_currency_iso_code(currency, 'CAD')
19+
end
20+
21+
it "detects currency based on currency passed as a block" do
22+
product = Product.new
23+
currency = product.send(:currency_for, :lambda_price, nil, ->(_) { 'CAD' })
24+
25+
expect_to_be_a_currency_instance(currency)
26+
expect_currency_iso_code(currency, 'CAD')
27+
end
28+
29+
it "detects currency based on currency passed explicitly" do
30+
product = Product.new
31+
currency = product.send(:currency_for, :bonus, nil, 'CAD')
32+
33+
expect_to_be_a_currency_instance(currency)
34+
expect_currency_iso_code(currency, 'CAD')
35+
end
36+
end
37+
38+
context "when falling back to a default or registered currency" do
39+
it "falls back to a registered currency" do
40+
product = Product.new
41+
currency = product.send(:currency_for, :amount, nil, nil)
42+
43+
expect_equal_currency(currency, Product.currency)
44+
end
45+
46+
it "falls back to a default currency" do
47+
transaction = Transaction.new
48+
currency = transaction.send(:currency_for, :amount, nil, nil)
49+
50+
expect_equal_currency(currency, Money.default_currency)
51+
end
52+
end
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)