Slide 1

Slide 1 text

An RSpec 3 Talk

Slide 2

Slide 2 text

a!/samphippen

Slide 3

Slide 3 text

RSpec RSpec ! ! RSpec 3

Slide 4

Slide 4 text

Upgrading

Slide 5

Slide 5 text

The easiest major gem upgrade you’ve ever done

Slide 6

Slide 6 text

The upgrade process

Slide 7

Slide 7 text

Be green and warning free on RSpec 2

Slide 8

Slide 8 text

gem “rspec”, “~> 2.99”

Slide 9

Slide 9 text

bundle install

Slide 10

Slide 10 text

fix warnings

Slide 11

Slide 11 text

gem “rspec”, “~> 3.0”

Slide 12

Slide 12 text

Done.

Slide 13

Slide 13 text

There is also Transpec

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Verifying doubles

Slide 16

Slide 16 text

Who’s ever stubbed an object’s interface wrong?

Slide 17

Slide 17 text

class Foo def bar(beers) “#{beers} bottles of beer” end end

Slide 18

Slide 18 text

allow(Foo.new).to receive(:bare)

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

class Foo def bar(beers) “#{beers} bottles of beer” end end

Slide 21

Slide 21 text

allow(Foo.new).to receive(:bar).with(1,2)

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Not enabled by default for upgrade installs

Slide 24

Slide 24 text

RSpec.configure do |config| config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end end

Slide 25

Slide 25 text

http://tinyurl.com/p6yk8ll

Slide 26

Slide 26 text

Verifying behaviour on pure doubles

Slide 27

Slide 27 text

double( :some_name_for_the_double, :call => :result )

Slide 28

Slide 28 text

instance_double( MyClass, :call => :result )

Slide 29

Slide 29 text

instance_double( MyClass, :call => :result )

Slide 30

Slide 30 text

instance_double( “MyClass”, :call => :result )

Slide 31

Slide 31 text

Let’s have some questions !!/samphippen [email protected]

Slide 32

Slide 32 text

Also class_double and object_double

Slide 33

Slide 33 text

Composable matchers

Slide 34

Slide 34 text

One Expectation Per Test

Slide 35

Slide 35 text

expect(json[“foo”]).to eq(bar) expect(json[“baz”]).to eq(quz) expect(json[“boz”][“buz”]).to match(id_regex)

Slide 36

Slide 36 text

expect(json[“foo”]).to eq(bar) expect(json[“baz”]).to eq(quz) expect(json[“boz”][“buz”]).to match(id_regex)

Slide 37

Slide 37 text

Could write separate tests

Slide 38

Slide 38 text

That could be expensive

Slide 39

Slide 39 text

expect(json).to match( “foo” => bar, “baz” => quz, “boz” => { “buz” => match(id_regex) } )

Slide 40

Slide 40 text

Match parts of hashes and arrays

Slide 41

Slide 41 text

{ “key” => { “key1” => value1, “key2” => value2, } }

Slide 42

Slide 42 text

expect(foo).to match( “key” => a_hash_including( “key2”=> value2 ) )

Slide 43

Slide 43 text

{ “key” => [1,2,3] }

Slide 44

Slide 44 text

expect(foo).to match( “key” => an_array_including(1) )

Slide 45

Slide 45 text

Compound expressions via and and or

Slide 46

Slide 46 text

expect(alphabet).to start_with("a") expect(alphabet).to end_with("z")

Slide 47

Slide 47 text

expect(alphabet) .to start_with(“a”) .and end_with("z")

Slide 48

Slide 48 text

expect(stoplight.color) .to eq(“red") .or eq(“green") .or eq("yellow")

Slide 49

Slide 49 text

Nearly all existing RSpec matchers are composable

Slide 50

Slide 50 text

Works with custom matchers

Slide 51

Slide 51 text

Rails

Slide 52

Slide 52 text

Let’s have some questions !!/samphippen [email protected]

Slide 53

Slide 53 text

OMG. Loading rails is the worst thing ever

Slide 54

Slide 54 text

files took 2.09 seconds to load

Slide 55

Slide 55 text

spec_helper is now split

Slide 56

Slide 56 text

spec_helper.rb Loads RSpec, not your app

Slide 57

Slide 57 text

rails_helper.rb Loads RSpec + whole rails app

Slide 58

Slide 58 text

Spec Types

Slide 59

Slide 59 text

RSpec rails used to be highly implicit

Slide 60

Slide 60 text

#spec/controllers/foo_controller_spec.rb describe FooController it “magically has get and assigns methods????” get :index expect(assigns[:foo]).to be true end end

Slide 61

Slide 61 text

#spec/controllers/foo_controller_spec.rb describe FooController, :type => :controller it “gets the methods from the metadata” get :index expect(assigns[:foo]).to be true end end

Slide 62

Slide 62 text

This applies to all Rails spec types

Slide 63

Slide 63 text

You can structure your specs however you like

Slide 64

Slide 64 text

If you badly need directory based spec inference

Slide 65

Slide 65 text

RSpec.configure do |config| config.infer_spec_type_from_file_location! end

Slide 66

Slide 66 text

http://tinyurl.com/kgllgtb

Slide 67

Slide 67 text

RSpec is trying to get less bad at patching methods on to everything for you

Slide 68

Slide 68 text

Sytnax

Slide 69

Slide 69 text

The “a.should == b” syntax is not gone

Slide 70

Slide 70 text

It is deprecated

Slide 71

Slide 71 text

I strongly advise you to use expect(a).to eq(b) everywhere

Slide 72

Slide 72 text

a.should == b expect(a).to eq(b)

Slide 73

Slide 73 text

a.should_receive(:foo) expect(a).to receive(:foo)

Slide 74

Slide 74 text

a.stub(:foo) allow(a).to receive(:foo)

Slide 75

Slide 75 text

expect { }.to raise_error ! #is the same

Slide 76

Slide 76 text

a.any_instance.stub(:foo) allow_any_instance_of(a).to receive(:foo)

Slide 77

Slide 77 text

a.any_instance.should_receive(:foo) expect_any_instance_of(a).to receive(:foo)

Slide 78

Slide 78 text

RSpec also now has a “no monkey patching” mode

Slide 79

Slide 79 text

RSpec 2 monkey patches #describe

Slide 80

Slide 80 text

describe “” do describe “” do end it “” do end end

Slide 81

Slide 81 text

RSpec.describe “” do describe “” do end it “” do end end

Slide 82

Slide 82 text

bare describe is still available in RSpec contexts

Slide 83

Slide 83 text

This one might trip you up

Slide 84

Slide 84 text

be_true be_false

Slide 85

Slide 85 text

be_truthy be_falsy be_falsey

Slide 86

Slide 86 text

be true be false

Slide 87

Slide 87 text

Let’s have some questions a!/samphippen [email protected]