15. test stub
describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
16. test stub
describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
17. test stub
describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
18. test stub
describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
19. mock object
describe Statement do
it quot;logs a message when printedquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
logger = mock(quot;loggerquot;)
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
20. mock object
describe Statement do
it quot;logs a message when printedquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
logger = mock(quot;loggerquot;)
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
21. mock object
describe Statement do
it quot;logs a message when printedquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
logger = mock(quot;loggerquot;)
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
22. mock object
describe Statement do
it quot;logs a message when printedquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
logger = mock(quot;loggerquot;)
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
23. mock object
describe Statement do
it quot;logs a message when printedquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
logger = mock(quot;loggerquot;)
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
25. describe Statement do
it quot;logs a message when printedquot; do
customer = Object.new
logger = Object.new
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
26. describe Statement do
it quot;logs a message when printedquot; do
customer = Object.new
logger = Object.new
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
27. describe Statement do
it quot;logs a message when printedquot; do
customer = Object.new
logger = Object.new
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
28. method stub
describe Statement do
it quot;logs a message when printedquot; do
customer = Object.new
logger = Object.new
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
29. describe Statement do
it quot;logs a message when printedquot; do
customer = Object.new
logger = Object.new
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
30. message expectation
describe Statement do
it quot;logs a message when printedquot; do
customer = Object.new
logger = Object.new
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
32. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = mock(quot;customerquot;)
statement = Statement.new(customer)
customer.should_receive(:name).and_return('Joe Customer')
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
33. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = mock(quot;customerquot;)
statement = Statement.new(customer)
customer.should_receive(:name).and_return('Joe Customer')
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
34. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = mock(quot;customerquot;)
statement = Statement.new(customer)
customer.should_receive(:name).and_return('Joe Customer')
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
35. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = mock(quot;customerquot;)
statement = Statement.new(customer)
customer.should_receive(:name).and_return('Joe Customer')
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
36. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = mock(quot;customerquot;)
statement = Statement.new(customer)
customer.should_receive(:name).and_return('Joe Customer')
statement.header.should == quot;Statement for Joe Customerquot;
end
end
message
expectation
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
37. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = mock(quot;customerquot;)
statement = Statement.new(customer)
customer.should_receive(:name).and_return('Joe Customer')
statement.header.should == quot;Statement for Joe Customerquot;
end
end
bound to
implementation
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
38. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
39. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
40. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
41. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
42. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
????
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
43. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
message
expectation
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
44. describe Statement do
it quot;uses the customer name in the headerquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
statement = Statement.new(customer)
statement.header.should == quot;Statement for Joe Customerquot;
end
end
bound to
implementation
class Statement
def header
quot;Statement for #{@customer.name}quot;
end
end
53. random values
class BoardTest < MiniTest::Unit::TestCase
def test_allows_move_to_last_square
board = Board.new(
:squares => 50,
:die => MiniTest::Mock.new.expect('roll', 2)
)
piece = Piece.new
board.place(piece, 48)
board.move(piece)
assert board.squares[48].contains?(piece)
end
end
54. time
describe Event do
it quot;is not happening before the start timequot; do
now = Time.now
start = now + 1
Time.stub(:now).and_return now
event = Event.new(:start => start)
event.should_not be_happening
end
end
61. strategies
describe Employee do
it quot;delegates pay() to payment strategyquot; do
payment_strategy = mock()
employee = Employee.new(payment_strategy)
payment_strategy.expects(:pay)
employee.pay
end
end
62. mixins/plugins
describe AgeIdentifiable do
describe quot;#can_vote?quot; do
it quot;raises if including does not respond to birthdatequot; do
object = Object.new
object.extend AgeIdentifiable
expect { object.can_vote? }.to raise_error(
/must supply a birthdate/
)
end
it quot;returns true if birthdate == 18 years agoquot; do
object = Object.new
stub(object).birthdate {18.years.ago.to_date}
object.extend AgeIdentifiable
object.can_vote?.should be(true)
end
end
end
64. side effects
describe Statement do
it quot;logs a message when printedquot; do
customer = stub(quot;customerquot;)
customer.stub(:name).and_return('Joe Customer')
logger = mock(quot;loggerquot;)
statement = Statement.new(customer, logger)
logger.should_receive(:log).with(/Joe Customer/)
statement.print
end
end
65. caching
describe ZipCode do
it quot;should only validate oncequot; do
validator = mock()
zipcode = ZipCode.new(quot;02134quot;, validator)
validator.should_receive(:valid?).with(quot;02134quot;).once.
and_return(true)
zipcode.valid?
zipcode.valid?
end
end
66. interface discovery
describe quot;thing I'm working onquot; do
it quot;does something with some assistancequot; do
thing_i_need = mock()
thing_i_am_working_on = ThingIAmWorkingOn.new(thing_i_need)
thing_i_need.should_receive(:help_me).and_return('what I need')
thing_i_am_working_on.do_something_complicated
end
end
92. partials in view specs
describe quot;/registrations/new.html.erbquot; do
before(:each) do
template.stub(:render).with(:partial => anything)
end
it quot;renders the registration navigationquot; do
template.should_receive(:render).with(:partial => 'nav')
render
end
it quot;renders the registration form quot; do
template.should_receive(:render).with(:partial => 'form')
render
end
end
93. conditional branches in
controller specs
describe quot;POST createquot; do
describe quot;with valid attributesquot; do
it quot;redirects to list of registrationsquot; do
registration = stub_model(Registration)
Registration.stub(:new).and_return(registration)
registration.stub(:save!).and_return(true)
post 'create'
response.should redirect_to(registrations_path)
end
end
end
94. conditional branches in
controller specs
describe quot;POST createquot; do
describe quot;with invalid attributesquot; do
it quot;re-renders the new formquot; do
registration = stub_model(Registration)
Registration.stub(:new).and_return(registration)
registration.stub(:save!).and_raise(
ActiveRecord::RecordInvalid.new(registration))
post 'create'
response.should render_template('new')
end
end
end
95. conditional branches in
controller specs
describe quot;POST createquot; do
describe quot;with invalid attributesquot; do
it quot;assigns the registrationquot; do
registration = stub_model(Registration)
Registration.stub(:new).and_return(registration)
registration.stub(:save!).and_raise(
ActiveRecord::RecordInvalid.new(registration))
post 'create'
assigns[:registration].should equal(registration)
end
end
end
96. shave a few lines but
leave a little stubble
http://github.com/dchelimsky/stubble
97. stubble
describe quot;POST createquot; do
describe quot;with valid attributesquot; do
it quot;redirects to list of registrationsquot; do
stubbing(Registration) do
post 'create'
response.should redirect_to(registrations_path)
end
end
end
end
98. stubble
describe quot;POST createquot; do
describe quot;with invalid attributesquot; do
it quot;re-renders the new formquot; do
stubbing(Registration, :as => :invalid) do
post 'create'
response.should render_template('new')
end
end
it quot;assigns the registrationquot; do
stubbing(Registration, :as => :invalid) do
|registration|
post 'create'
assigns[:registration].should equal(registration)
end
end
end
end
99. chains
describe UsersController do
it quot;GET 'best_friend'quot; do
member = stub_model(User)
friends = stub()
friend = stub_model(User)
User.stub(:find).and_return(member)
member.stub(:friends).and_return(friends)
friends.stub(:favorite).and_return(friend)
get :best_friend, :id => '37'
assigns[:friend].should equal(friend)
end
end
100. chains
describe UsersController do
it quot;GET 'best_friend'quot; do
friend = stub_model(User)
User.stub_chain(:find, :friends, :favorite).
and_return(friend)
get :best_friend, :id => '37'
assigns[:friend].should equal(friend)
end
end
111. false positives
describe RegistrationsController do
describe quot;GET 'pending'quot; do
it quot;finds the pending registrationsquot; do
pending_registration = stub_model(Registration)
Registration.should_receive(:pending).
and_return([pending_registration])
get 'pending'
assigns[:registrations].should == [pending_registration]
end
end
end
class RegistrationsController < ApplicationController
def pending
@registrations = Registration.pending
end
end
112. false positives
describe RegistrationsController do
describe quot;GET 'pending'quot; do
it quot;finds the pending registrationsquot; do
pending_registration = stub_model(Registration)
Registration.should_receive(:pending).
and_return([pending_registration])
get 'pending'
assigns[:registrations].should == [pending_registration]
end
end
end
class RegistrationsController < ApplicationController
def pending
@registrations = Registration.pending
end
end
113. false positives
describe Registration do
describe quot;#pendingquot; do
it quot;finds pending registrationsquot; do
Registration.create!
Registration.create!(:pending => true)
Registration.pending.should have(1).item
end
end
end
class Registration < ActiveRecord::Base
named_scope :pending, :conditions => {:pending => true}
end
114. false positives
describe Registration do
describe quot;#pendingquot; do
it quot;finds pending registrationsquot; do
Registration.create!
Registration.create!(:pending => true)
Registration.pending.should have(1).item
end
end
end
class Registration < ActiveRecord::Base
named_scope :pending, :conditions => {:pending => true}
end
115. false positives
describe Registration do
describe quot;#pendingquot; do
it quot;finds pending registrationsquot; do
Registration.create!
Registration.create!(:pending => true)
Registration.pending_confirmation.should have(1).item
end
end
end
class Registration < ActiveRecord::Base
named_scope :pending_confirmation, :conditions => {:pending => true}
end
116. false positives
describe Registration do
describe quot;#pendingquot; do
it quot;finds pending registrationsquot; do
Registration.create!
Registration.create!(:pending => true)
Registration.pending_confirmation.should have(1).item
end
end
end
class Registration < ActiveRecord::Base
named_scope :pending_confirmation, :conditions => {:pending => true}
end