-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdealer_spec.rb
executable file
·105 lines (82 loc) · 2.58 KB
/
dealer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'dealer'
require 'player'
describe Dealer do
subject(:dealer) { Dealer.new }
it "calls super with a default name/empty bankroll" do
expect(dealer.name).to eq("dealer")
expect(dealer.bankroll).to eq(0)
end
it "is a Player subclass" do
expect(dealer).to be_a(Player)
end
it "does not place bets" do
expect do
dealer.place_bet(dealer, 100)
end.to raise_error("Dealer doesn't bet")
end
describe "#play_hand" do
let(:dealer_hand) { double("hand") }
let(:deck) { double("deck") }
before do
dealer.hand = dealer_hand
end
it "does not hit on seventeen" do
allow(dealer_hand).to receive_messages(
:busted? => false,
:points => 17
)
expect(dealer_hand).not_to receive(:hit)
dealer.play_hand(deck)
end
it "hits until seventeen acheived" do
allow(dealer_hand).to receive_messages(:busted? => false)
# need to use a block to give points, because we'll hit hand and
# points will change
points = 12
allow(dealer_hand).to receive(:points) do
# returns `points` defined in the outside local variable. The
# `points` variable is said to be *captured*.
points
end
expect(dealer_hand).to receive(:hit).with(deck).exactly(3).times do
# changes `points` variable above, faking addition of new
# cards.
points += 2
end
dealer.play_hand(deck)
end
it "stops when busted" do
points = 16
allow(dealer_hand).to receive(:points) { points }
allow(dealer_hand).to receive(:busted?) { points > 21 }
expect(dealer_hand).to receive(:hit).once.with(deck) do
points = 22
end
dealer.play_hand(deck)
end
end
context "with a player" do
let(:player) { double("player", :hand => player_hand) }
let(:dealer_hand) { double("dealer_hand") }
let(:player_hand) { double("player_hand") }
before(:each) do
dealer.hand = dealer_hand
allow(player).to receive_messages(:hand => player_hand)
dealer.take_bet(player, 100)
end
it "records bets" do
expect(dealer.bets).to eq({ player => 100 })
end
it "does not pay losers (or ties)" do
expect(player_hand).to receive(:beats?).with(dealer_hand).and_return(false)
expect(player).not_to receive(:pay_winnings)
dealer.pay_bets
end
it "does pay winners" do
expect(player_hand).to receive(:beats?).with(dealer_hand).and_return(true)
# wins twice the bet
expect(player).to receive(:pay_winnings).with(200)
dealer.pay_bets
end
end
end