5. describe ShowController, “index” do
context “when a TV show has no public videos” do
it “should not display any shows” do
Show.should_receive(:all)
.with(:select => “id,name,video_count”,
:conditions => “shows.video_count > 0”)
.and_return([])
get ‘index’
response.body.should_not(match(/
#{show.name}))
end
end
end
7. describe ShowController, “index” do
context “when a TV show has no public videos” do
it “should not display any shows” do
Show.should_receive(:all)
.with(:select => “id,name,video_count”,
:conditions => “shows.video_count > 0”)
.and_return([])
get ‘index’
response.body.should_not(match(/
#{show.name}))
end
end
end
10. module Codebreaker
describe Game do
describe “#start” do
it “sends a welcome message” do
output = double(‘output’)
game = Game.new(output)
output.should_receive(:puts)
.with(‘Welcome to Codebreaker!’)
game.start
end
end
end
end
11. module Codebreaker
class Game
def initialize(output)
@output = output
end
def start
@output.puts(“Welcome to
Codebreaker!”)
end
end
end
24. describe ShowController, “index” do
context “when a TV show has no public videos” do
it “should not display any shows” do
Show.should_receive(:all)
.with(:select => “id,name,video_count”,
:conditions => “shows.video_count > 0”)
.and_return([])
get ‘index’
response.body.should_not(match(/
#{show.name}))
end
end
end
Asserting on State!
28. “The key in making great and growable
systems is much more to design how
its modules communicate rather than
what their internal behaviors should
be.”
-Alan Kay
Inventor of Smalltalk
53. describe TicketMachineInterface do
it “reserves the number of tickets inputted when
user submits the request” do
request_handler = double(‘request_handler’)
request_handler.should_receive(:reserve).with(55)
machine =
TicketMachineInterface.new(request_handler)
machine.number_pressed(5)
machine.number_pressed(5)
machine.submit_request
end
end
54. describe TicketMachineInterface do
it “reserves the number of tickets inputted when
user submits the request” do
request_handler = double(‘request_handler’)
request_handler.should_receive(:reserve).with(55)
machine =
TicketMachineInterface.new(request_handler)
machine.number_pressed(5)
machine.number_pressed(5)
machine.submit_request
end
end
55. describe TicketMachineInterface do
it “reserves the number of tickets inputted when
user submits the request” do
request_handler = double(‘request_handler’)
request_handler.should_receive(:reserve).with(55)
machine =
TicketMachineInterface.new(request_handler)
machine.number_pressed(5)
machine.number_pressed(5)
machine.submit_request
end
end
57. describe TicketMachineInterface do
it “reserves the number of tickets inputted when
user submits the request” do
request_handler = double(‘request_handler’)
request_handler.should_receive(:reserve).with(55)
machine =
TicketMachineInterface.new(request_handler)
machine.number_pressed(5)
machine.number_pressed(5)
machine.submit_request
end
end
73. describe TicketMachineInterface do
it “reserves the correct number of tickets when a
number is pressed two times before submitting” do
machine =
TicketMachineInterface.new
machine.number_pressed(5)
machine.number_pressed(5)
machine.submit_request
end
end
74. describe TicketMachineInterface do
it “reserves the number of tickets inputted when
user submits the request” do
request_handler = double(‘request_handler’)
request_handler.should_receive(:reserve).with(55)
machine =
TicketMachineInterface.new(request_handler)
machine.number_pressed(5)
machine.number_pressed(5)
machine.submit_request
end
end
Not the actual object
being implemented, but
the role that object is
going to play
82. module Codebreaker
describe Game do
describe “#start” do
it “sends a welcome message” do
output = double(‘output’)
game = Game.new(output)
output.should_receive(:puts)
.with(‘Welcome to Codebreaker!’)
game.start
end
end
end
end
83. module Codebreaker
class Game
def initialize(output)
@output = output
end
def start
@output.puts(“Welcome to
Codebreaker!”)
end
end
end
We’re coupling it to
the actual Ruby
standard output
The responsibility of this
game is to talk to a
command-line interface
86. module Codebreaker
describe Game do
describe “#start” do
it “sends a welcome message” do
displayer = double(‘displayer’)
game = Game.new(displayer)
displayer.should_receive(:display)
.with(‘Welcome to Codebreaker!’)
game.start
end
end
end
end
87. module Codebreaker
class Game
def initialize(displayer)
@displayer = displayer
end
def start
@displayer.display(“Welcome
to Codebreaker!”)
end
end
end
104. describe AuctionMessageTranslator
it “notifies bid details when current price message
received” do
listener = double(‘event_listener’)
listener.should_receive(:current_price)
.with(192, 7)
translator = AuctionMessageTranslator.new(listener)
message = Message.new(“SOLVersion: 1.1;
Event: Price; CurrentPrice: 192; Increment: 7;
Bidder: Someone Else”)
translator.process_message(message)
end
end
105. class AuctionMessageTranslator
def initialize(listener)
@listener = listener
end
def process_message(message)
event = unpack_event_from(message)
if event.type == “CLOSE”
@listener.auction_closed
else
@listener.current_price(event.current_price,
event.increment)
end
end
private
def unpack_event_from(message)
AuctionMessageEvent.new(message)
end
end
106. class AuctionMessageTranslator
def initialize(listener)
@listener = listener
end
def process_message(message)
event = unpack_event_from(message)
if event.type == “CLOSE”
@listener.auction_closed
else
@listener.current_price(event.current_price,
event.increment)
end
end
private
def unpack_event_from(message)
AuctionMessageEvent.new(message)
end
end
107. class AuctionMessageTranslator
def initialize(listener)
@listener = listener
end
def process_message(message)
event = unpack_event_from(message)
if event.type == “CLOSE”
@listener.auction_closed
else
@listener.current_price(event.current_price,
event.increment)
end
end
private
def unpack_event_from(message)
AuctionMessageEvent.new(message)
end
end