How RSpec Works

How RSpec Works

C727e695ffdc7bf95f9f1a97da592648?s=128

Penelope Phippen

April 19, 2019
Tweet

Transcript

  1. 3.

    Sam Phippen Any code (except code from RSpec itself) in

    this talk is copyright Google under Apache V2
  2. 10.

    Sam Phippen specs: diff-lcs (1.3) rspec (3.8.0) rspec-core (~> 3.8.0)

    rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-core (3.8.0) rspec-support (~> 3.8.0)
  3. 40.

    Sam Phippen RSpec.describe "eg1" do it "e1" it "e2" context

    "eg1_1" do it "e3" end context "eg2_1" do it "e3" end end
  4. 72.

    Sam Phippen class Match < BaseMatcher def match(expected, actual) return

    true if values_match?( expected, actual) actual.match(expected) end end
  5. 73.

    Sam Phippen class Match < BaseMatcher def match(expected, actual) return

    true if values_match?( expected, actual) actual.match(expected) end end
  6. 76.

    Sam Phippen def self.values_match?(expected, actual) if Hash === actual return

    hashes_match?(expected, actual) if Hash === expected elsif Array === expected && Enumerable === actual && !(Struct === actual) return arrays_match?(expected, actual.to_a) end return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on 1.9+, raise # ArgumentError for `expected === actual`. false end end
  7. 78.

    Sam Phippen def self.values_match?(e, a) if Hash === a return

    hashes_match?(e, a) if Hash === e elsif Array === e && Enumerable === a && !(Struct === a) return arrays_match?( e, a) end
  8. 79.

    Sam Phippen def self.values_match?(e, a) if Hash === a return

    hashes_match?(e, a) if Hash === e elsif Array === e && Enumerable === a && !(Struct === a) return arrays_match?( e, a) end
  9. 80.

    Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size

    != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end
  10. 81.

    Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size

    != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end
  11. 82.

    Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size

    != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end
  12. 83.

    Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size

    != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end
  13. 84.

    Sam Phippen def self.hashes_match?( expected_hash, actual_hash) return false if expected_hash.size

    != actual_hash.size expected_hash.all? do |e_k, e_v| actual_value = actual_hash.fetch(e_k) { return false } values_match?(e_v, actual_value) end end
  14. 85.

    Sam Phippen def self.values_match?(e, a) if Hash === a return

    hashes_match?(e, a) if Hash === e elsif Array === e && Enumerable === a && !(Struct === a) return arrays_match?( e, a) end
  15. 86.

    Sam Phippen def self.arrays_match?( expected_list, actual_list) return false if expected_list.size!=actual_list.size

    expected_list.zip(actual_list).all? do |expected, actual| values_match?(expected, actual) end end
  16. 87.

    Sam Phippen def self.arrays_match?( expected_list, actual_list) return false if expected_list.size!=actual_list.size

    expected_list.zip(actual_list).all? do |expected, actual| values_match?(expected, actual) end end
  17. 88.

    Sam Phippen def self.arrays_match?( expected_list, actual_list) return false if expected_list.size!=actual_list.size

    expected_list.zip(actual_list).all? do |expected, actual| values_match?(expected, actual) end end
  18. 90.

    Sam Phippen def values_match(expected, actual) … (the code we saw

    earlier) return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on # 1.9+, raise # ArgumentError for `expected === actual`. false end end
  19. 91.

    Sam Phippen def values_match(expected, actual) … (the code we saw

    earlier) return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on # 1.9+, raise # ArgumentError for `expected === actual`. false end end
  20. 92.

    Sam Phippen def values_match(expected, actual) … (the code we saw

    earlier) return true if expected == actual begin expected === actual rescue ArgumentError # Some objects, like 0-arg lambdas on # 1.9+, raise # ArgumentError for `expected === actual`. false end end
  21. 93.

    Sam Phippen /a/ === “a” lambda { |x| x ==

    1 } === 1 Any Rspec matcher === a value it matches
  22. 107.

    Sam Phippen RSpec.describe "mocking" do it “adds a foo method"

    do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end
  23. 108.

    Sam Phippen RSpec.describe "mocking" do it “adds a foo method"

    do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end
  24. 119.

    Sam Phippen RSpec.describe "mocking" do it “adds a foo method"

    do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end
  25. 131.

    Sam Phippen RSpec.describe "mocking" do it “adds a foo method"

    do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end
  26. 138.

    Sam Phippen RSpec.describe "mocking" do it “adds a foo method"

    do allow(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end
  27. 139.

    Sam Phippen At the end of the test, the MethodDouble

    puts the original method back on the object
  28. 141.

    Sam Phippen RSpec.describe "mocking" do it “adds a foo method"

    do expect(foo).to receive(:bees) expect(foo.bees).to eq(nil) end end