Permalink
Browse files

through end of chapter 14

  • Loading branch information...
1 parent 5a670a5 commit 6e584583f7fca3bd5bf21d738e6c0fd46fe7bf5d @marick committed Dec 22, 2009
View
@@ -1,4 +1,5 @@
require 'test/unit'
+require 'flexmock'
require 'flexmock/test_unit'
require 'shoulda'
require 'assert2'
@@ -9,6 +10,9 @@
# test can state what is to happen before stating what the mock should
# receive.
class Test::Unit::TestCase
+
+ attr_accessor :meaning_at_this_moment
+
def because(&block)
@because = block
self
@@ -28,29 +32,37 @@ def behold_nothing_happens!
behold! {}
end
+end
+
+
+class FlexMock::Expectation
+ # For now, just token is recorded for the state of the test after some
+ # particular message is received.
+
+ alias old_verify_call verify_call
- # Easy mock setup
-
- # Use like this:
- # Timeslice.new(mocks(:use_source, :procedure_source, :hash_maker))
- #
- # ...
- #
- # @use_source.should_expect(...)
- #
- #
- # See util/test-support.rb for how tested classes should arrange
- # for easy mocking.
-
- def mocks(*symbols)
- hash = {}
- symbols.each do | symbol |
- name = symbol.to_s
- a_mock = flexmock(name)
- instance_variable_set("@#{name}", a_mock)
- hash[symbol] = a_mock
- end
- hash
+ def verify_call(*args)
+ @when_expectation_fulfilled.call if @when_expectation_fulfilled
+ old_verify_call(*args)
end
+ def which_means(meaning)
+ the_whole_test = mock.flexmock_container
+ @when_expectation_fulfilled = lambda {
+ the_whole_test.meaning_at_this_moment = meaning
+ }
+ self
+ end
+
+ def when(meaning)
+ the_whole_test = mock.flexmock_container
+ @when_expectation_fulfilled = lambda {
+ FlexMock.check("Everything up to this point in the test was supposed to mean " +
+ "'#{meaning}', but it meant " +
+ "'#{the_whole_test.meaning_at_this_moment}'.") {
+ meaning == the_whole_test.meaning_at_this_moment
+ }
+ }
+ end
end
+
@@ -1,15 +1,29 @@
+require 'app/auction-message-translator'
+
class AuctionSniper
+ include AuctionMessageTranslator::PriceSource
+
def initialize(auction, sniper_listener)
@auction = auction
@sniper_listener = sniper_listener
+ @is_winning
end
def auction_closed
- @sniper_listener.sniper_lost
+ if @is_winning
+ @sniper_listener.sniper_won
+ else
+ @sniper_listener.sniper_lost
+ end
end
def current_price(price, increment, source)
- @auction.bid(price+increment)
- @sniper_listener.sniper_bidding
+ @is_winning = (source == FROM_SNIPER)
+ if @is_winning
+ @sniper_listener.sniper_winning
+ else
+ @auction.bid(price+increment)
+ @sniper_listener.sniper_bidding
+ end
end
end
@@ -10,10 +10,14 @@ def sniper_lost
show_status(MainWindow::STATUS_LOST)
end
- def sniper_winning # This seems to have popped up from nowhere.
+ def sniper_winning
show_status(MainWindow::STATUS_WINNING)
end
+ def sniper_won
+ show_status(MainWindow::STATUS_WON)
+ end
+
private
def show_status(status)
@@ -18,7 +18,7 @@ Feature: How the sniper works as an auction progresses
Given the sniper has joined an ongoing auction
When the sniper responds to a bid of 1000 by rebidding an increment of 98
Then the auction reports the sniper has bid 1098 with an increment of 97
- And the sniper shows that it's won the auction
+ And the sniper shows that it's winning the auction
When the auction closes
Then the sniper shows that it's won the auction
@@ -10,6 +10,10 @@
@driver.should have_sniper_status MainWindow::STATUS_WON
end
+Then /^the sniper shows that it.s winning the auction$/ do
+ @driver.should have_sniper_status MainWindow::STATUS_WINNING
+end
+
Then /^the sniper shows that it.s bidding in the auction$/ do
@driver.should have_sniper_status MainWindow::STATUS_BIDDING
end
@@ -2,23 +2,39 @@
require 'sandbox'
require 'microtests/testutil'
+require 'app/auction-message-translator'
require 'app/auction-sniper'
class AuctionSniperTests < Test::Unit::TestCase
+ include AuctionMessageTranslator::PriceSource
+
def setup
@auction = flexmock('auction')
@sniper_listener = flexmock("sniper listener")
@sniper = AuctionSniper.new(@auction, @sniper_listener)
end
- should "report that sniper lost when the auction closes" do
+ should "report that sniper lost when the auction closes immediately" do
during {
@sniper.auction_closed
}.behold! {
@sniper_listener.should_receive(:sniper_lost).at_least.once
}
end
+ should "report that sniper lost when the auction closes while someone else has high bid" do
+ @auction.should_ignore_missing
+ during {
+ @sniper.current_price(123, 45, FROM_OTHER_BIDDER)
+ @sniper.auction_closed
+ }.behold! {
+ @sniper_listener.should_receive(:sniper_bidding).
+ which_means("someone else is in the lead")
+ @sniper_listener.should_receive(:sniper_lost).at_least.once.
+ when("someone else is in the lead")
+ }
+ end
+
should "bid higher (and so report) when a new price arrives" do
price = 1001
increment = 25
@@ -30,4 +46,25 @@ def setup
}
end
+ should "report that the sniper is winning when the current price came from it" do
+ during {
+ @sniper.current_price(123, 45, FROM_SNIPER)
+ }.behold! {
+ @sniper_listener.should_receive(:sniper_winning).at_least.once
+ }
+ end
+
+ should "report that sniper lost when the auction closes while the sniper is winning" do
+ @auction.should_ignore_missing
+ during {
+ @sniper.current_price(123, 45, FROM_SNIPER)
+ @sniper.auction_closed
+ }.behold! {
+ @sniper_listener.should_receive(:sniper_winning).
+ which_means("the sniper is winning")
+ @sniper_listener.should_receive(:sniper_won).at_least.once.
+ when("the sniper is winning")
+ }
+ end
+
end

0 comments on commit 6e58458

Please sign in to comment.