REQUIREMENT
REQUIREMENT
REQUIREMENT
REQUIREMENT
REQUIREMENT
TEST
REQUIREMENT
REQUIREMENT
CODE
Is it to attract Ruby job ??
DSL for testing Ruby code
Widely popular
It doesn't matter which framework you used as long as you write a test
GREEN
REFACTOR
RED
As a husband I need to have at least Rp 10.000 in my wallet's balance.
My wife asked me to buy a mango using my balance.
RED
RSpec.describe Wallet do
subject { Wallet.new(1) }
it "should have default balance" do
expect(subject.balance).to eq(10000)
end
endRSpec.describe BuyFruit do
let(:wallet) { Wallet.new(1) }
it "should have brought fruit" do
shopping = BuyFruit.call(wallet, 9000)
expect(shopping[:status]).to eq(true)
expect(shopping[:wallet].balance).to eq(1000)
end
it "should have not brought fruit" do
shopping = BuyFruit.call(wallet, 12000)
expect(shopping[:status]).to be_falsey
expect(shopping[:wallet].balance).to eq(10000)
end
endGREEN
class Wallet
attr_reader :id, :balance
def initialize(id, balance = 10000)
@id = id
@balance = balance
end
def balance=(n)
@balance = n
end
end
class BuyFruit
def self.call(wallet, price)
options = {}
options[:wallet] = wallet
options[:status] = false
if options[:wallet].balance >= price
options[:wallet].balance -= price
options[:status] = true
end
options
end
end
REFACTOR
class DecreaseBalance
def self.call(wallet, amount)
options = {}
options[:wallet] = wallet
options[:status] = false
if options[:wallet].balance >= amount
options[:wallet].balance -= amount
options[:status] = true
end
options
end
end
class BuyFruit
def self.call(wallet, price)
options = {}
transaction = DecreaseBalance.(wallet, price)
options[:status] = transaction[:status]
options[:wallet] = transaction[:wallet]
options
end
end