The document discusses the benefits of test-driven development (TDD) and automated testing. It provides an example of using TDD to develop a simple calculator application, starting with writing tests for the desired functionality before implementing the code. As tests are added and code is refined, TDD helps ensure quality, catch errors, and drive good design. When practiced regularly through techniques like TDD and continuous integration, automated testing allows software to be developed and maintained efficiently as a team over time.
16. console
$ rspec spec/calculator_spec.rb!
!
/path/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:481:in
`load_missing_constant': Unable to autoload constant Calculator, expected /path/
calculator.rb to define it (LoadError)!
! from /path/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:180:in
`const_missing'!
! from /path/spec/calculator_spec.rb:3:in `<top (required)>'!
! from /path/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'!
! from /path/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in
load_spec_files'!
! from /path/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'!
! from /path/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in
`load_spec_files’ …
18. console
$ rspec spec/calculator_spec.rb!
!
Run options: include {:focus=>true}!
!
All examples were filtered out; ignoring {:focus=>true}!
!
Calculator!
.sum!
sum 2 and 2!
!
Finished in 0.20637 seconds (files took 4.72 seconds to load)!
1 example, 0 failures!
!
Randomized with seed 49032
19. calculator_spec.rb
RSpec.describe Calculator do!
!
describe '.sum' do!
!
it 'sum 2 and 2' do!
expect(Calculator.sum(2, 2)).to eq(4)!
end!
!
it 'sum 3 and 2' do!
expect(Calculator.sum(3, 2)).to eq(5)!
end!
end!
end!
20. console
1) Calculator.sum sum 3 and 2!
Failure/Error: expect(Calculator.sum(3, 2)).to eq(5)!
!
expected: 5!
got: 4!
!
(compared using ==)!
# ./spec/calculator_spec.rb:12:in `block (3 levels) in <top (required)>'!
!
Finished in 0.24562 seconds (files took 4.67 seconds to load)!
2 examples, 1 failure!
!
Failed examples:!
!
rspec ./spec/calculator_spec.rb:11 # Calculator.sum sum 3 and 2
22. console
Run options: include {:focus=>true}!
!
All examples were filtered out; ignoring {:focus=>true}!
!
Calculator!
.sum!
sum 2 and 2!
sum 3 and 2!
!
Finished in 0.20995 seconds (files took 4.78 seconds to load)!
2 examples, 0 failures!
!
Randomized with seed 64301