Slide 1

Slide 1 text

Fix Simple, stupid testing framework for Ruby March 1, 2016

Slide 2

Slide 2 text

github.com/cyril Cyril Kato twitter.com/cyri_

Slide 3

Slide 3 text

Chess Gungi Shogi Xiangqi

Slide 4

Slide 4 text

Let’s begin with a survey...

Slide 5

Slide 5 text

Who likes RSpec?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Even for simple code?

Slide 8

Slide 8 text

Even for simple code? Let’s take an example

Slide 9

Slide 9 text

0-example/app.rb APP = 42

Slide 10

Slide 10 text

0-example/app_spec.rb require_relative 'app' require 'rspec' RSpec.describe APP do it { expect(described_class).to be 42 } end

Slide 11

Slide 11 text

0-example/app_small_test.rb require_relative 'app' fail unless 42.equal?(APP)

Slide 12

Slide 12 text

Both tests are passing: ➜ ruby 0-example/app_small_test.rb ➜ rspec 0-example/app_spec.rb . Finished in 0.00098 seconds 1 example, 0 failures

Slide 13

Slide 13 text

Both tests are passing. But...

Slide 14

Slide 14 text

Testing with RSpec, the result was processed over... rspec 3.4.0 63 LOC

Slide 15

Slide 15 text

Testing with RSpec, the result was processed over... rspec 3.4.0 rspec-core 3.4.3 63 LOC 7 389 LOC +

Slide 16

Slide 16 text

Testing with RSpec, the result was processed over... rspec 3.4.0 rspec-core 3.4.3 rspec-expectations 3.4.0 63 LOC 7 389 LOC 3 917 LOC + +

Slide 17

Slide 17 text

Testing with RSpec, the result was processed over... rspec 3.4.0 rspec-core 3.4.3 rspec-expectations 3.4.0 rspec-mocks 3.4.1 63 LOC 7 389 LOC 3 917 LOC 4 106 LOC + + +

Slide 18

Slide 18 text

Testing with RSpec, the result was processed over... rspec 3.4.0 rspec-core 3.4.3 rspec-expectations 3.4.0 rspec-mocks 3.4.1 rspec-support 3.4.1 63 LOC 7 389 LOC 3 917 LOC 4 106 LOC 1 624 LOC + + + +

Slide 19

Slide 19 text

Testing with RSpec, the result was processed over... rspec 3.4.0 rspec-core 3.4.3 rspec-expectations 3.4.0 rspec-mocks 3.4.1 rspec-support 3.4.1 diff-lcs 1.2.5 63 LOC 7 389 LOC 3 917 LOC 4 106 LOC 1 624 LOC 1 405 LOC + + + + +

Slide 20

Slide 20 text

Testing with RSpec, the result was processed over... rspec 3.4.0 rspec-core 3.4.3 rspec-expectations 3.4.0 rspec-mocks 3.4.1 rspec-support 3.4.1 diff-lcs 1.2.5 63 LOC 7 389 LOC 3 917 LOC 4 106 LOC 1 624 LOC 1 405 LOC + + + + + 18 504 LOC!!

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

In the other case, with Ruby’s fail(*args)

Slide 25

Slide 25 text

1 LOC

Slide 26

Slide 26 text

Both tests are passing. Butthe second is more relevant.

Slide 27

Slide 27 text

complexity(testing tool) SHOULD be < complexity(code)

Slide 28

Slide 28 text

Code to test Testing tool

Slide 29

Slide 29 text

BTW

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

ANYWAY Here’s another example

Slide 32

Slide 32 text

require 'rspec' RSpec.describe(-42) do describe '#abs' do it { expect(described_class.abs).to equal 42 } end end

Slide 33

Slide 33 text

require 'rspec' RSpec.describe(-42) do describe '#abs' do it { expect(described_class.abs).to equal 42 } end end Same method, but different behavior

Slide 34

Slide 34 text

require 'rspec' RSpec.describe(-42) do describe '#abs' do it { expect(described_class.abs).to equal 42 } end end I don’t care about class or instance methods

Slide 35

Slide 35 text

require 'rspec' RSpec.describe(-42) do describe '#abs' do it { expect(described_class.abs).to equal 42 } end end We need to repeat the code

Slide 36

Slide 36 text

ZOMG FIX new specing framework

Slide 37

Slide 37 text

require 'fix' Fix.describe(-42) do on :abs do it { MUST equal 42 } end end

Slide 38

Slide 38 text

require 'fix' Fix.describe(-42) do on :abs do it { MUST equal 42 } end end The front object

Slide 39

Slide 39 text

require 'fix' Fix.describe(-42) do on :abs do it { MUST equal 42 } end end An event

Slide 40

Slide 40 text

require 'fix' Fix.describe(-42) do on :abs do it { MUST equal 42 } end end A subject

Slide 41

Slide 41 text

require 'fix' Fix.describe(-42) do on :abs do it { MUST equal 42 } end end An absolute requirement

Slide 42

Slide 42 text

RFC 2119 compliant

Slide 43

Slide 43 text

The key words "MUST", "MUST NOT", "SHOULD", "SHOULD NOT", and "MAY" in this document are to be interpreted as described in RFC 2119.

Slide 44

Slide 44 text

What about code isolation

Slide 45

Slide 45 text

require 'rspec' greeting = 'Hello world!' RSpec.describe greeting do context 'Alice' do before { greeting.gsub!('world', 'Alice') } it { expect(greeting).to eql 'Hello Alice!' } end context 'Bob' do before { greeting.gsub!('world', 'Bob') } it { expect(greeting).to eql 'Hello Bob!' } end end

Slide 46

Slide 46 text

require 'rspec' greeting = 'Hello world!' RSpec.describe greeting do context 'Alice' do before { greeting.gsub!('world', 'Alice') } it { expect(greeting).to eql 'Hello Alice!' } end context 'Bob' do before { greeting.gsub!('world', 'Bob') } it { expect(greeting).to eql 'Hello Bob!' } end end No isolation of the code to avoid the side effects

Slide 47

Slide 47 text

➜ rspec 2-example/greeting_spec.rb .F Failures: 1) Hello world! Bob should eql "Hello Bob!" Failure/Error: it { expect(greeting).to eql 'Hello Bob!' } expected: "Hello Bob!" got: "Hello Alice!" Finished in 0.01521 seconds 2 examples, 1 failure

Slide 48

Slide 48 text

require 'fix' greeting = 'Hello world!' Fix.describe greeting do context 'Alice' do on :gsub!, 'world', 'Alice' do it { MUST eql 'Hello Alice!' } end end context 'Bob' do on :gsub!, 'world', 'Bob' do it { MUST eql 'Hello Bob!' } end end end

Slide 49

Slide 49 text

➜ ruby 2-example/greeting_fix.rb .. Ran 2 tests in 0.005775 seconds 100% compliant - 0 infos, 0 failures, 0 errors

Slide 50

Slide 50 text

With Fix, no side effects outside contexts

Slide 51

Slide 51 text

no particular Style Guide

Slide 52

Slide 52 text

Don't make me think

Slide 53

Slide 53 text

describe "#foo" or describe ".foo"?

Slide 54

Slide 54 text

describe or context?

Slide 55

Slide 55 text

it or specify?

Slide 56

Slide 56 text

Don't get me wrong

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

So I decided to rebuild it

Slide 59

Slide 59 text

In a more adequat way

Slide 60

Slide 60 text

github.com/cyril/r_spec and I call it r_spec

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Well, let’s try it!

Slide 63

Slide 63 text

app = 'OMGLOL' def app.equal?(*) true end require 'rspec' RSpec.describe app do it { expect(app).to be 42 } end

Slide 64

Slide 64 text

app = 'OMGLOL' def app.equal?(*) true end require 'rspec' RSpec.describe app do it { expect(app).to be 42 } end rspec 3-example/strange_app_spec.rb . Finished in 0.00097 seconds 1 example, 0 failures

Slide 65

Slide 65 text

What did you RSpec?

Slide 66

Slide 66 text

app = 'OMGLOL' def app.equal?(*) true end require 'rspec' RSpec.describe app do it { expect(app).to be 42 } end rspec 3-example/strange_app_spec.rb . Finished in 0.00097 seconds 1 example, 0 failures

Slide 67

Slide 67 text

app = 'OMGLOL' def app.equal?(*) true end require 'rspec' RSpec.describe app do it { expect(app).to be 42 } end rspec 3-example/strange_app_spec.rb . Finished in 0.00097 seconds 1 example, 0 failures Let’s fix that!

Slide 68

Slide 68 text

app = 'OMGLOL' def app.equal?(*) true end require 'rspec' RSpec.describe app do it { expect(app).to be 42 } end

Slide 69

Slide 69 text

app = 'OMGLOL' def app.equal?(*) true end require 'r_spec' RSpec.describe app do it { expect(app).to be 42 } end

Slide 70

Slide 70 text

app = 'OMGLOL' def app.equal?(*) true end require 'r_spec' RSpec.describe app do it { expect(app).to be 42 } end rspec 3-example/strange_app_spec.rb F 1. Failure: Expected "OMGLOL" to be 42. /Users/cyril/parisrb/3-example/strange_ app_spec.rb:10:in `block (2 levels) in ' Ran 1 tests in 0.000195 seconds 0% compliant - 0 infos, 1 failures, 0 errors

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Code in the slides github.com/cyril/parisrb

Slide 73

Slide 73 text

Any questions?

Slide 74

Slide 74 text

Happy specing!!!! github.com/fixrb fixrb.dev