TDD - Back to the Future

Agnieszka Małaszkiewicz at Fractal Soft

Notes: - 1985 - Do you remember it? - look young - my childhood - TDD like this movie (old, out of date, boring)
Notes: - me in numbers - 5 years ago

250
20

3500

86%

Notes: - based on my example and this presentation - convince you that TDD can be done - can be useful, beneficial - can be fun
Notes: - you look like that right now - kind of... - you don't look sure about that
Notes: - shall we start? - I think so - let's check what we have here

Quality Assurance
?
Tests

“QA and Testing both have to make software better, but QA enhances the quality via an improvement of development process and testing enhances it via finding bugs.”

Quality Assurance
>
Tests

Test-Driven Development

TDD

Two examples

Example 1

Follow your test

#### Create method ```ruby def flush? end ```
#### Add argument ```ruby def flush?(array) end ```

Add first logic

```ruby def flush?(array) true end ```

Run test

```bash $ rspec spec/lib/flush_spec.rb Randomized with seed 40116 . Finished in 0.01189 seconds (files took 0.65796 seconds to load) 1 example, 0 failures ```

CSI: NY

Triangulation based on CSI: NY series

Add more logic

```ruby def flush?(array) array.uniq.size == 1 end ```

Run tests

```bash $ rspec spec/lib/flush_spec.rb Randomized with seed 33907 .. Finished in 0.01092 seconds (files took 0.57891 seconds to load) 2 examples, 0 failures ```
#### Write code ```ruby class BaseClass def self.validates_presence_of(name) end def self.validates_numericality_of(name) end def valid? end end ```
#### Write code ```ruby class BaseClass def self.validates_presence_of(name) end def self.validates_numericality_of(name) end def valid? end def errors end end ```
#### Run test ```bash $ ruby item_test.rb Run options: --seed 35483 # Running: . Finished in 0.000730s, 1369.7898 runs/s, 12328.1085 assertions/s. 1 runs, 9 assertions, 0 failures, 0 errors, 0 skips ``` Notes: - finally, test pass - 5-10 min - working code - time for refactoring
## Small fixes
```ruby class BaseClass def self.validates_presence_of(name) end def self.validates_numericality_of(name) end def valid? @errors_list = [] @errors_list << "name can't be blank" if @name.nil? @errors_list << "price must be number" unless @price.is_a?(Integer) @errors_list.size == 0 end def errors @errors_list end end ``` Notes: - trivial implementation - **good** - is working - **good** - readable - **bad** - hard to maintenance - **bad** - not agile (no adapting to changes)
```ruby class BaseClass attr_reader :errors def self.validates_presence_of(name) @validators ||= [] @validators << PresenceValidator.new(name) end def self.validates_numericality_of(name) @validators ||= [] @validators << NumericallityValidator.new(name) end def valid? @errors = [] self.class.instance_variable_get(:@validators).each do |item| errors << item.error_message unless item.valid?(send(item.name)) end errors.empty? end private attr_writer :errors end ``` Notes: - more agile, elastic - easy to expand - still readable, maybe less

Running tests

How looks console when all tests passed - rainbow style

Nyan Cat

Running test with Nyan Cat style

My project is different...

Gilded Rose Kata

Kata

github.com/emilybache/GildedRose-Refactoring-Kata

Saving trap
Angry hare
Happy turtle
Fractal Soft logo

Fractal Soft

We're hiring!
Rails Girls heart

Rails Girls

Woman on Rails logo

womanonrails.com/presentations

### Bibliography #### Books - [Test Driven Development: By Example - Ken Beck](https://amzn.to/2HXdSHy) - [Working Effectively with Legacy Code - Michael Feathers](https://amzn.to/2v36esF) - [Clean Code: A Handbook of Agile Software Craftsmanship - Robert C. Martin](https://amzn.to/395gxuM) - [Design Patterns in Ruby - Russ Olsen](https://amzn.to/32qK8fQ) - [Practical Object-Oriented Design in Ruby: An Agile Primer - Sandi Metz](https://amzn.to/2Vk9Vox) - [The Pragmatic Programmer: From Journeyman to Master - Andrew Hund, David Thomas](https://amzn.to/2TgmfTR) #### Presentations - [All the Little Things by Sandi Metz](https://www.youtube.com/watch?v=8bZh5LMaSmE) - [Nothing is Something by Sandi Metz](https://www.youtube.com/watch?v=OMPfEXIlTVE) - [Best Ruby on Rails refactoring talks](https://infinum.co/the-capsized-eight/best-ruby-on-rails-refactoring-talks) - [Are you an Egoistic Programmer? - presentation about refactoring](https://womanonrails.com/presentation-refactoring-egoistic-programmer/#1)
Woman on Rails logotype

Agnieszka Małaszkiewicz

agnieszka (at) fractalsoft (dot) org