File: formatting_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (110 lines) | stat: -rw-r--r-- 3,522 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'rspec/matchers/fail_matchers'
require 'support/doubled_classes'

RSpec.describe "Test doubles format well in failure messages" do
  include RSpec::Matchers::FailMatchers
  include RSpec::Support::Spec::DiffHelpers

  RSpec::Matchers.define :format_in_failures_as do |expected|
    match do |dbl|
      values_match?(expected, actual_formatting(dbl))
    end

    def actual_formatting(double)
      expect(1).to eq(double)
    rescue RSpec::Expectations::ExpectationNotMetError => e
      e.message[/expected: (.*)$/, 1]
    else
      raise "Did not fail as expected"
    end
  end

  describe "`double`" do
    context "with a name" do
      specify '#<Double "Name">' do
        expect(double "Book").to format_in_failures_as('#<Double "Book">')
      end

      it 'formats the name as a symbol if that was how it was provided' do
        expect(double :book).to format_in_failures_as('#<Double :book>')
      end
    end

    context "without a name" do
      specify '#<Double (anonymous)>' do
        expect(double).to format_in_failures_as('#<Double (anonymous)>')
      end
    end

    it 'avoids sending `instance_variable_get` to the double as it may be stubbed' do
      dbl = double("Book")
      expect(dbl).not_to receive(:instance_variable_get)
      expect(dbl).to format_in_failures_as('#<Double "Book">')
    end
  end

  describe "`instance_double(SomeClass)`" do
    context "with a name" do
      specify '#<InstanceDouble(SomeClass) "Name">' do
        expect(instance_double(LoadedClass, "Book")).to format_in_failures_as('#<InstanceDouble(LoadedClass) "Book">')
      end
    end

    context "without a name" do
      specify '#<InstanceDouble(SomeClass) (anonymous)>' do
        expect(instance_double(LoadedClass)).to format_in_failures_as('#<InstanceDouble(LoadedClass) (anonymous)>')
      end
    end

    it 'avoids sending `instance_variable_get` to the double as it may be stubbed' do
      dbl = instance_double(LoadedClass, "Book")
      expect(dbl).not_to receive(:instance_variable_get)
      expect(dbl).to format_in_failures_as('#<InstanceDouble(LoadedClass) "Book">')
    end
  end

  describe "`class_double(SomeClass)`" do
    context "with a name" do
      specify '#<ClassDouble(SomeClass) "Name">' do
        expect(class_double(LoadedClass, "Book")).to format_in_failures_as('#<ClassDouble(LoadedClass) "Book">')
      end
    end

    context "without a name" do
      specify '#<ClassDouble(SomeClass) (anonymous)>' do
        expect(class_double(LoadedClass)).to format_in_failures_as('#<ClassDouble(LoadedClass) (anonymous)>')
      end
    end
  end

  describe "`object_double([])`" do
    context "with a name" do
      specify '#<ObjectDouble([]) "Name">' do
        expect(object_double([], "Name")).to format_in_failures_as('#<ObjectDouble([]) "Name">')
      end
    end

    context "without a name" do
      specify '#<ObjectDouble([]) (anonymous)>' do
        expect(object_double([])).to format_in_failures_as('#<ObjectDouble([]) (anonymous)>')
      end
    end
  end

  it 'formats the doubles when they appear in data structures and diffs' do
    allow(RSpec::Expectations.configuration).to receive(:color?).and_return(false)

    foo = double("Foo")
    bar = double("Bar")

    expect {
      expect([foo]).to include(bar)
    }.to fail_with(<<-EOS.gsub(/^\s+\|/, ''))
      |expected [#<Double "Foo">] to include #<Double "Bar">
      |Diff:
      |@@ #{one_line_header} @@
      |-[#<Double "Bar">]
      |+[#<Double "Foo">]
    EOS
  end
end