Professional Documents
Culture Documents
Testing Key
Testing Key
http://bit.ly/codemash-testing
Jim Weirich
Chief Scientist / EdgeCase
jim@edgecase.com
@jimweirich
Testing:
TDD/BDD?
Unit Testing?
Any Testing?
•Unit Tests
•Checkin Tests
•Checkin Tests
Your
Application
X
Auth Client
Lib
Auth
Service
Mocked
Authorization
Client Lib
get_connection
(many times)
Connction Pool
get_connection
release_connection
get_connection
(once)
DB
get_connection
release_connection
get_connection
(many times)
Connction Pool
get_connection
release_connection
get_connection
(once)
X
Mock DB
get_connection get_connection
release_connection release_connection
get_connection
(many times)
Code Under Test
Connction Pool
get_connection
release_connection
get_connection
(once)
X
Mock DB
get_connection get_connection
release_connection release_connection
get_connection
(many times)
Code Under Test
Connction Pool
get_connection
release_connection
Mock
get_connection
(once)
X
Mock DB
get_connection get_connection
release_connection release_connection
price
Service Trip
Contract
discount
price
amount_charged * 1
price
Service Trip
Contract
discount
price_per_visit
amount_charged * 1
price
Service Trip
Contract
discount
price_per_visit
amount_charged * 1
price
Service Trip
Contract
discount
price_per_visit
amount_charged * 1
price_per_visit
Service Trip
Contract
discount
price_per_visit
amount_charged * 1
price_per_visit
Service Trip
Contract
discount
price_per_visit
amount_charged * 1
• Use a mock to
• Mock an external service
• Verify a protocol
• Don't use a mock to:
• Avoid creating complex Objects
Factory / sqlite3
Factory Build
Mocks d
Factory / Attrs
Raw New
Seconds
def test_total_cost
order = Order.create(
:items => [Item.create(:cost => 10)])
assert_equal 10, order.total_cost
end
def test_order_fails_with_bad_supplier
order = Order.new(:supplier => :bad)
assert ! order.save
end
def test_order_fails_with_bad_supplier
order = Order.new(:supplier => :bad)
assert ! order.valid?
end
assert ! order.valid?
assert model.errors.on(:supplier)
assert_match(/(invalid|bad).*supplier/i,
model.errors.on(field).to_s,
end
controller.send(:load_personal_data)
end
end
Pathelogical
it "loads the personal data Coupling
from from the" do
Text
controller.stub!(:params).
and_return(:person => 'John Doe')
PersonalDataService.
should_receive(:get_personal_data).
with(:owner_id)
controller.send(:load_personal_data)
end
end
controller.send(:load_personal_data)
end
end
controller.send(:load_personal_data)
end
end
describe Stack do
context "nearly empty" do
Text
subject { a_stack_with_one_item }
context "when popped" do
before { subject.pop }
it { should be_empty }
end
end
end
describe Stack do
context "nearly empty" do
Given(:stack) { a_stack_with_one_item }
When { stack.pop Text
}
Then { stack.should be_empty }
end
end
Necessary
Sufficient
Jim Weirich
Chief Scientist / EdgeCase
jim@edgecase.com
@jimweirich