self-testingcodein ruby · tdd practicesofwriting testsbeforethe code...
TRANSCRIPT
![Page 1: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/1.jpg)
Self-testing Code in
RubyGiovanni Sakti
Starqle
![Page 2: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/2.jpg)
What is Self-testing code?
![Page 3: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/3.jpg)
Self-testing code
Code that have built-in tests
The tests serve as a binding contract
The tests can be run arbitrarily
![Page 4: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/4.jpg)
What is TDD? How it differs from self-testing code?
![Page 5: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/5.jpg)
TDD
Practices of writing tests before the
code
Ensure that the code is self-tested
It is, however, optional to do TDD to
write self-testing code
![Page 6: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/6.jpg)
TDD
But some companies enforce TDD
because TDD enforces YAGNI principle
![Page 7: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/7.jpg)
TDD
We'll see why...
![Page 8: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/8.jpg)
TDD Steps
Write a test
Run the test, it should fail
Write code just enough to pass the
test
Run the test
Repeat
![Page 9: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/9.jpg)
TDD & YAGNI
Because we only write just enough code
to pass the test, there will be no
unnecessary codes
![Page 10: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/10.jpg)
Test in Ruby
There are several tools for doing testing
in ruby
![Page 11: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/11.jpg)
Test in Ruby
RSpec
Minitest
test-unit
![Page 12: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/12.jpg)
Test in Ruby
Let's try using RSpec
![Page 13: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/13.jpg)
RSpec Install
% gem install rspec
![Page 14: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/14.jpg)
RSpec Help
% rspec --help
![Page 15: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/15.jpg)
Now let's do TDD practice using RSpec
![Page 16: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/16.jpg)
TDD with RSpec (1)
Create a simple test of program that we
want to create
# game_spec.rb
RSpec.describe Game do describe "#score" do it "returns 0 for new game" do game = Game.new expect(game.score).to eq(0) end endend
![Page 17: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/17.jpg)
TDD with RSpec (2)
Run the example and watch it fail
% rspec game_spec.rb uninitialized constant Object::Game (NameError)
![Page 18: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/18.jpg)
TDD with RSpec (3)
Now write just enough code to make it pass
# game.rb
class Game attr_reader :score
def initialize @score = 0 endend
![Page 19: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/19.jpg)
TDD with RSpec (3)
Now write just enough code to make it pass
# game_spec.rb
require './game'...
![Page 20: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/20.jpg)
TDD with RSpec (4)
Run the example and the test shall pass
% rspec game_spec.rb --color --format doc
Game #score returns 0 for all gutter game
Finished in 0.00057 seconds1 example, 0 failures
![Page 21: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/21.jpg)
TDD with RSpec (5)
Repeat with new features
![Page 22: Self-testingCodein Ruby · TDD Practicesofwriting testsbeforethe code Ensurethatthecodeisself-tested Itis,however, optionaltodoTDDto writeself-testingcode](https://reader033.vdocument.in/reader033/viewer/2022052006/601a3076b9e9a167501bea74/html5/thumbnails/22.jpg)
Thanks